Skip to content

Commit

Permalink
Add support of errInfo (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
v-anyukov authored May 13, 2023
1 parent 1437736 commit 95b4756
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/mongo/error.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Mongo.Error do
alias Mongo.Events

defexception [:message, :code, :host, :fail_command, :error_labels, :resumable, :retryable_reads, :retryable_writes, :not_writable_primary_or_recovering]
defexception [:message, :code, :host, :fail_command, :error_labels, :resumable, :retryable_reads, :retryable_writes, :not_writable_primary_or_recovering, :error_info]

@exceeded_time_limit 262
@failed_to_satisfy_read_preference 133
Expand Down Expand Up @@ -90,7 +90,8 @@ defmodule Mongo.Error do
resumable: boolean,
retryable_reads: boolean,
retryable_writes: boolean,
not_writable_primary_or_recovering: boolean
not_writable_primary_or_recovering: boolean,
error_info: map()
}

def message(e) do
Expand Down Expand Up @@ -125,6 +126,7 @@ defmodule Mongo.Error do
retryable_writes: retryable_writes,
not_writable_primary_or_recovering: not_writable_primary_or_recovering
}
|> maybe_add_error_info(doc)
end

def exception(message: message, code: code) do
Expand Down Expand Up @@ -212,6 +214,13 @@ defmodule Mongo.Error do
def fail_command?(%Mongo.Error{fail_command: fail_command}) do
fail_command
end

defp maybe_add_error_info(error, %{"errInfo" => info}) do
error
|> Map.put_new(:error_info, info)
end

defp maybe_add_error_info(error, _), do: error
end

defmodule Mongo.WriteError do
Expand Down
32 changes: 32 additions & 0 deletions test/mongo/errors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,36 @@ defmodule Mongo.ErrorsTest do
assert false == Error.not_writable_primary_or_recovering?(the_error, [])
assert false == Error.should_retry_read(the_error, [ping: 1], [])
end

test "error info", %{pid: top} do
cmd = [
collMod: "validated",
validator: [
"$jsonSchema": %{
"bsonType" => "object",
"properties" => %{
"_id" => %{
"bsonType" => "objectId"
},
"text" => %{
"bsonType" => "string"
},
"isDone" => %{
"bsonType" => "bool"
}
},
"required" => ["text", "isDone"],
"additionalProperties" => false
}
]
]

# Let's play safe
Mongo.drop_collection(top, "validated")
Mongo.create(top, "validated")

Mongo.command!(top, cmd)

assert match?({:error, %Mongo.WriteError{write_errors: [%{"code" => 121, "errInfo" => %{"details" => _}}]}}, Mongo.insert_one(top, "validated", %{"text" => 11}))
end
end

0 comments on commit 95b4756

Please sign in to comment.