Skip to content

Commit 95b4756

Browse files
authored
Add support of errInfo (#187)
1 parent 1437736 commit 95b4756

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/mongo/error.ex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Mongo.Error do
22
alias Mongo.Events
33

4-
defexception [:message, :code, :host, :fail_command, :error_labels, :resumable, :retryable_reads, :retryable_writes, :not_writable_primary_or_recovering]
4+
defexception [:message, :code, :host, :fail_command, :error_labels, :resumable, :retryable_reads, :retryable_writes, :not_writable_primary_or_recovering, :error_info]
55

66
@exceeded_time_limit 262
77
@failed_to_satisfy_read_preference 133
@@ -90,7 +90,8 @@ defmodule Mongo.Error do
9090
resumable: boolean,
9191
retryable_reads: boolean,
9292
retryable_writes: boolean,
93-
not_writable_primary_or_recovering: boolean
93+
not_writable_primary_or_recovering: boolean,
94+
error_info: map()
9495
}
9596

9697
def message(e) do
@@ -125,6 +126,7 @@ defmodule Mongo.Error do
125126
retryable_writes: retryable_writes,
126127
not_writable_primary_or_recovering: not_writable_primary_or_recovering
127128
}
129+
|> maybe_add_error_info(doc)
128130
end
129131

130132
def exception(message: message, code: code) do
@@ -212,6 +214,13 @@ defmodule Mongo.Error do
212214
def fail_command?(%Mongo.Error{fail_command: fail_command}) do
213215
fail_command
214216
end
217+
218+
defp maybe_add_error_info(error, %{"errInfo" => info}) do
219+
error
220+
|> Map.put_new(:error_info, info)
221+
end
222+
223+
defp maybe_add_error_info(error, _), do: error
215224
end
216225

217226
defmodule Mongo.WriteError do

test/mongo/errors_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,36 @@ defmodule Mongo.ErrorsTest do
7575
assert false == Error.not_writable_primary_or_recovering?(the_error, [])
7676
assert false == Error.should_retry_read(the_error, [ping: 1], [])
7777
end
78+
79+
test "error info", %{pid: top} do
80+
cmd = [
81+
collMod: "validated",
82+
validator: [
83+
"$jsonSchema": %{
84+
"bsonType" => "object",
85+
"properties" => %{
86+
"_id" => %{
87+
"bsonType" => "objectId"
88+
},
89+
"text" => %{
90+
"bsonType" => "string"
91+
},
92+
"isDone" => %{
93+
"bsonType" => "bool"
94+
}
95+
},
96+
"required" => ["text", "isDone"],
97+
"additionalProperties" => false
98+
}
99+
]
100+
]
101+
102+
# Let's play safe
103+
Mongo.drop_collection(top, "validated")
104+
Mongo.create(top, "validated")
105+
106+
Mongo.command!(top, cmd)
107+
108+
assert match?({:error, %Mongo.WriteError{write_errors: [%{"code" => 121, "errInfo" => %{"details" => _}}]}}, Mongo.insert_one(top, "validated", %{"text" => 11}))
109+
end
78110
end

0 commit comments

Comments
 (0)