Skip to content

Commit

Permalink
add the errInfo field
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 8, 2022
1 parent c1f31ca commit 9f77126
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/cryomongo/collection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ class Mongo::Collection
labels = last_error_object["errorLabels"]?.try { |l|
Array(String).from_bson(l)
} || [] of String
raise Mongo::Error::Command.new(code, code_name, msg, error_labels: Set(String).new(labels))
details = last_error_object["errInfo"]?.try &.as(BSON)
raise Mongo::Error::Command.new(code, code_name, msg, details, error_labels: Set(String).new(labels))
end

result["value"]?.try &.as(BSON)
Expand Down
8 changes: 5 additions & 3 deletions src/cryomongo/error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module Mongo
class Error::Command < Error::Server
getter code : Int32
getter code_name : String?
getter details : BSON?

# See: https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#not-master-and-node-is-recovering
RECOVERING_CODES = {11600, 11602, 13436, 189, 91}
Expand All @@ -110,7 +111,7 @@ module Mongo
# See: https://github.com/mongodb/specifications/blob/f1fcb6aa9751e5ed7eb8e64c0f08f1edf10a859a/source/change-streams/change-streams.rst#resumable-error
RESUMABLE_CODES = {63, 133, 150, 234, 13388, 133} + RETRYABLE_CODES

def initialize(code, @code_name, message, *, @error_labels = Set(String).new)
def initialize(code, @code_name, message, @details, *, @error_labels = Set(String).new)
@code = code.try &.as(Int32) || 0
@message = message.try(&.as(String)) || ""
end
Expand Down Expand Up @@ -162,7 +163,8 @@ module Mongo
err_code_name = error["codeName"]?.try &.as(String)
err_msg = error["errmsg"]?.try &.as(String)
err_labels = error["errorLabels"]?.try { |labels| Array(String).from_bson(labels) } || [] of String
@errors << Error::Command.new(err_code, err_code_name, err_msg, error_labels: Set(String).new(err_labels))
details = error["errInfo"]?.try &.as(BSON)
@errors << Error::Command.new(err_code, err_code_name, err_msg, details, error_labels: Set(String).new(err_labels))
}
end

Expand All @@ -178,7 +180,7 @@ module Mongo
def initialize(error : BSON)
@code = error["code"]?.try(&.as(Int).to_i32) || 0
@message = error["errmsg"]?.try(&.as(String)) || ""
@details = error["err_info"]?.try &.as(BSON)
@details = error["errInfo"]?.try &.as(BSON)
end

def failed_or_timeout?
Expand Down
3 changes: 2 additions & 1 deletion src/cryomongo/messages/op_msg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ struct Mongo::Messages::OpMsg < Mongo::Messages::Part
err_code_name = self.body["codeName"]?.try &.as(String)
err_code = self.body["code"]?
err_labels = self.body["errorLabels"]?.try { |labels| Array(String).from_bson(labels) } || [] of String
Mongo::Error::Command.new(err_code, err_code_name, err_msg, error_labels: Set(String).new(err_labels))
details = self.body["errInfo"]?.try &.as(BSON)
Mongo::Error::Command.new(err_code, err_code_name, err_msg, details, error_labels: Set(String).new(err_labels))
end
end

Expand Down

0 comments on commit 9f77126

Please sign in to comment.