Exception: Google::Cloud::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/google/cloud/errors.rb

Overview

Base google-cloud exception class.

Class Method Summary collapse

Class Method Details

.from_error(error) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/google/cloud/errors.rb', line 22

def self.from_error error
  klass = if error.respond_to? :code
            grpc_error_class_for error.code
          elsif error.respond_to? :status_code
            gapi_error_class_for error.status_code
          else
            self
          end
  klass.new error.message
end

.gapi_error_class_for(http_status_code) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/google/cloud/errors.rb', line 46

def self.gapi_error_class_for http_status_code
  # The http status codes mapped to their error classes.
  { 400 => InvalidArgumentError, # FailedPreconditionError/OutOfRangeError
    401 => UnauthenticatedError,
    403 => PermissionDeniedError,
    404 => NotFoundError,
    409 => AlreadyExistsError, # AbortedError
    429 => ResourceExhaustedError,
    499 => CanceledError,
    500 => InternalError, # UnknownError/DataLossError
    501 => UnimplementedError,
    503 => UnavailableError,
    504 => DeadlineExceededError
  }[http_status_code] || self
end

.grpc_error_class_for(grpc_error_code) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/google/cloud/errors.rb', line 34

def self.grpc_error_class_for grpc_error_code
  # The gRPC status code 0 is for a successful response.
  # So there is no error subclass for a 0 status code, use current class.
  [self, CanceledError, UnknownError, InvalidArgumentError,
   DeadlineExceededError, NotFoundError, AlreadyExistsError,
   PermissionDeniedError, ResourceExhaustedError, FailedPreconditionError,
   AbortedError, OutOfRangeError, UnimplementedError, InternalError,
   UnavailableError, DataLossError, UnauthenticatedError
  ][grpc_error_code] || self
end