Class: Couchbase::Protostellar::ErrorHandling Private

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/protostellar/error_handling.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

TYPE_URL_PRECONDITION_FAILURE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"type.googleapis.com/google.rpc.PreconditionFailure"
TYPE_URL_RESOURCE_INFO =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"type.googleapis.com/google.rpc.ResourceInfo"
TYPE_URL_ERROR_INFO =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"type.googleapis.com/google.rpc.ErrorInfo"
TYPE_URL_BAD_REQUEST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"type.googleapis.com/google.rpc.BadRequest"

Class Method Summary collapse

Class Method Details

.convert_rpc_status(rpc_status, request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/couchbase/protostellar/error_handling.rb', line 34

def self.convert_rpc_status(rpc_status, request)
  handle_rpc_status(rpc_status, request, set_context: false).error
end

.handle_grpc_error(grpc_error, request, set_context: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/couchbase/protostellar/error_handling.rb', line 38

def self.handle_grpc_error(grpc_error, request, set_context: true)
  request.context = {} if request.context.nil? && set_context

  rpc_status = grpc_error.to_rpc_status
  if rpc_status.nil?
    # Binary RPC status not provided, create an RPC status with the code and message from the error
    rpc_status = Google::Rpc::Status.new(code: grpc_error.code, message: grpc_error.details)
  end

  handle_rpc_status(rpc_status, request, set_context: set_context) unless rpc_status.nil?
end

.handle_rpc_status(rpc_status, request, set_context: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/couchbase/protostellar/error_handling.rb', line 70

def self.handle_rpc_status(rpc_status, request, set_context: true)
  detail_block = {}
  message = rpc_status.message

  # Decode the detail block
  rpc_status.details&.each do |d|
    type_url = d.type_url

    if type_url == TYPE_URL_PRECONDITION_FAILURE
      precondition_failure = Google::Rpc::PreconditionFailure.decode(d.value)
      detail_block[:precondition_failure] = precondition_failure
    end

    if type_url == TYPE_URL_RESOURCE_INFO
      resource_info = Google::Rpc::ResourceInfo.decode(d.value)
      detail_block[:resource_info] = resource_info
    end

    if type_url == TYPE_URL_ERROR_INFO
      error_info = Google::Rpc::ErrorInfo.decode(d.value)
      detail_block[:error_info] = error_info
    end

    if type_url == TYPE_URL_BAD_REQUEST
      bad_request = Google::Rpc::BadRequest.decode(d.value)
      detail_block[:bad_request] = bad_request
    end
  end

  populate_context(rpc_status, detail_block, request) if set_context

  behaviour =
    case rpc_status.code
    when Google::Rpc::Code::NOT_FOUND
      case detail_block[:resource_info].resource_type
      when "document"
        RequestBehaviour.fail(Couchbase::Error::DocumentNotFound.new(message, request.error_context))
      when "queryindex", "searchindex"
        RequestBehaviour.fail(Couchbase::Error::IndexNotFound.new(message, request.error_context))
      when "bucket"
        RequestBehaviour.fail(Couchbase::Error::BucketNotFound.new(message, request.error_context))
      when "scope"
        RequestBehaviour.fail(Couchbase::Error::ScopeNotFound.new(message, request.error_context))
      when "collection"
        RequestBehaviour.fail(Couchbase::Error::CollectionNotFound.new(message, request.error_context))
      when "path"
        RequestBehaviour.fail(Couchbase::Error::PathNotFound.new(message, request.error_context))
      end

    when Google::Rpc::Code::ALREADY_EXISTS
      case detail_block[:resource_info].resource_type
      when "document"
        RequestBehaviour.fail(Couchbase::Error::DocumentExists.new(message, request.error_context))
      when "queryindex", "searchindex"
        RequestBehaviour.fail(Couchbase::Error::IndexExists.new(message, request.error_context))
      when "bucket"
        RequestBehaviour.fail(Couchbase::Error::BucketExists.new(message, request.error_context))
      when "scope"
        RequestBehaviour.fail(Couchbase::Error::ScopeExists.new(message, request.error_context))
      when "collection"
        RequestBehaviour.fail(Couchbase::Error::CollectionExists.new(message, request.error_context))
      when "path"
        RequestBehaviour.fail(Couchbase::Error::PathExists.new(message, request.error_context))
      end

    when Google::Rpc::Code::INVALID_ARGUMENT
      RequestBehaviour.fail(Couchbase::Error::InvalidArgument.new(message, request.error_context))

    when Google::Rpc::Code::ABORTED
      case detail_block[:error_info].reason
      when "CAS_MISMATCH"
        RequestBehaviour.fail(Couchbase::Error::CasMismatch.new(message, request.error_context))
      end

    when Google::Rpc::Code::FAILED_PRECONDITION
      case detail_block[:precondition_failure].violations[0].type
      when "LOCKED"
        Retry::Orchestrator.maybe_retry(request, Retry::Reason::KV_LOCKED)
      when "DOC_TOO_DEEP"
        RequestBehaviour.fail(Couchbase::Error::PathTooDeep.new(message, request.error_context))
      when "DOC_NOT_JSON"
        RequestBehaviour.fail(Couchbase::Error::DocumentNotJson.new(message, request.error_context))
      when "PATH_MISMATCH"
        RequestBehaviour.fail(Couchbase::Error::PathMismatch.new(message, request.error_context))
      when "WOULD_INVALIDATE_JSON"
        RequestBehaviour.fail(Couchbase::Error::ValueInvalid.new(message, request.error_context))
      when "PATH_VALUE_OUT_OF_RANGE"
        RequestBehaviour.fail(Couchbase::Error::NumberTooBig.new(message, request.error_context))
      when "VALUE_TOO_LARGE"
        RequestBehaviour.fail(Couchbase::Error::ValueTooLarge.new(message, request.error_context))
      end

    when Google::Rpc::Code::UNIMPLEMENTED
      RequestBehaviour.fail(Couchbase::Error::FeatureNotAvailable.new(message, request.error_context))

    when Google::Rpc::Code::UNAUTHENTICATED
      RequestBehaviour.fail(Couchbase::Error::AuthenticationFailure.new(message, request.error_context))

    when Google::Rpc::Code::PERMISSION_DENIED
      if detail_block[:resource_info].resource_type == "user"
        RequestBehaviour.fail(Couchbase::Error::AuthenticationFailure.new(message, request.error_context))
      else
        RequestBehaviour.fail(Couchbase::Error::PermissionDenied.new(message, request.error_context))
      end

    when Google::Rpc::Code::CANCELLED
      RequestBehaviour.fail(Couchbase::Error::RequestCanceled.new(message, request.error_context))

    when Google::Rpc::Code::DEADLINE_EXCEEDED
      if request.idempotent
        RequestBehaviour.fail(Couchbase::Error::UnambiguousTimeout.new(message, request.error_context))
      else
        RequestBehaviour.fail(Couchbase::Error::AmbiguousTimeout.new(message, request.error_context))
      end

    when Google::Rpc::Code::INTERNAL
      RequestBehaviour.fail(Couchbase::Error::InternalServerFailure.new(message, request.error_context))

    when Google::Rpc::Code::UNAVAILABLE
      Retry::Orchestrator.maybe_retry(request, Retry::Reason::SOCKET_NOT_AVAILABLE)

    when Google::Rpc::Code::OK
      RequestBehaviour.success
    end

  if behaviour.nil?
    RequestBehaviour.fail(Couchbase::Error::CouchbaseError.new(message, request.error_context))
  else
    behaviour
  end
end

.populate_context(rpc_status, detail_block, request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/couchbase/protostellar/error_handling.rb', line 50

def self.populate_context(rpc_status, detail_block, request)
  request.context[:server] = rpc_status.message

  unless detail_block[:precondition_failure].nil?
    request.context[:precondition_violation] = detail_block[:precondition_failure].violations[0].type
  end

  unless detail_block[:resource_info].nil?
    request.context[:resource_type] = detail_block[:resource_info].resource_type
    unless detail_block[:resource_info].resource_name.empty?
      request.context[:resource_name] =
        detail_block[:resource_info].resource_name
    end
  end

  return if detail_block[:error_info].nil?

  request.context[:reason] = detail_block[:error_info].reason
end