class Google::Cloud::Error

Base google-cloud exception class.

Public Class Methods

from_error(error) click to toggle source

@private Create a new error object from a client error

# File lib/google/cloud/errors.rb, line 186
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) click to toggle source

@private Identify the subclass for a Google API Client error

# File lib/google/cloud/errors.rb, line 212
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
    412 => FailedPreconditionError,
    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) click to toggle source

@private Identify the subclass for a gRPC error

# File lib/google/cloud/errors.rb, line 198
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
new(msg = nil) click to toggle source

Construct a new Google::Cloud::Error object, optionally passing in a message.

@param msg [String, nil] an error message

Calls superclass method
# File lib/google/cloud/errors.rb, line 28
def initialize msg = nil
  super
end

Public Instance Methods

body() click to toggle source

Returns the value of `body` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over HTTP/REST.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 53
def body
  return nil unless cause.respond_to? :body
  cause.body
end
code() click to toggle source

Returns the value of `code` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over gRPC.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 79
def code
  return nil unless cause.respond_to? :code
  cause.code
end
details() click to toggle source

Returns the value of `details` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over gRPC.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 92
def details
  return nil unless cause.respond_to? :details
  cause.details
end
domain() click to toggle source

Returns the value of `domain` from the `::Google::Rpc::ErrorInfo` object, if it exists in the `status_details` array.

This is typically present on errors originating from calls to an API over gRPC.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 154
def domain
  return nil unless error_info.respond_to? :domain
  error_info.domain
end
error_info() click to toggle source

Returns the `::Google::Rpc::ErrorInfo` object present in the `status_details` or `details` array, given that the following is true:

* either `status_details` or `details` exists and is an array
* there is exactly one `::Google::Rpc::ErrorInfo` object in that array.

Looks in `status_details` first, then in `details`.

@return [::Google::Rpc::ErrorInfo, nil]

# File lib/google/cloud/errors.rb, line 131
def error_info
  @error_info ||= begin
    check_property = lambda do |prop|
      if prop.is_a? Array
        error_infos = prop.find_all { |status| status.is_a?(::Google::Rpc::ErrorInfo) }
        if error_infos.length == 1
          error_infos[0]
        end
      end
    end

    check_property.call(status_details) || check_property.call(details)
  end
end
error_metadata() click to toggle source

Returns the value of `metadata` from the `::Google::Rpc::ErrorInfo` object, if it exists in the `status_details` array.

This is typically present on errors originating from calls to an API over gRPC.

@return [Hash, nil]

# File lib/google/cloud/errors.rb, line 180
def error_metadata
  return nil unless error_info.respond_to? :metadata
  error_info.metadata.to_h
end
header() click to toggle source

Returns the value of `header` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over HTTP/REST.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 66
def header
  return nil unless cause.respond_to? :header
  cause.header
end
metadata() click to toggle source

Returns the value of `metadata` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over gRPC.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 105
def metadata
  return nil unless cause.respond_to? :metadata
  cause.metadata
end
reason() click to toggle source

Returns the value of `reason` from the `::Google::Rpc::ErrorInfo` object, if it exists in the `status_details` array.

This is typically present on errors originating from calls to an API over gRPC.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 167
def reason
  return nil unless error_info.respond_to? :reason
  error_info.reason
end
status_code() click to toggle source

Returns the value of `status_code` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over HTTP/REST.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 40
def status_code
  return nil unless cause.respond_to? :status_code
  cause.status_code
end
status_details() click to toggle source

Returns the value of `status_details` from the underlying cause error object, if both are present. Otherwise returns `nil`.

This is typically present on errors originating from calls to an API over gRPC.

@return [Object, nil]

# File lib/google/cloud/errors.rb, line 118
def status_details
  return nil unless cause.respond_to? :status_details
  cause.status_details
end