Class: Gruf::Client::ErrorFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/client/error_factory.rb

Overview

Translates exceptions into Gruf::Client::Errors

Instance Method Summary collapse

Constructor Details

#initialize(default_class: nil, deserializer_class: nil, metadata_key: nil) ⇒ ErrorFactory

Returns a new instance of ErrorFactory.

Parameters:

  • default_class (Class) (defaults to: nil)
  • deserializer_class (Class) (defaults to: nil)
  • metadata_key (String|Symbol) (defaults to: nil)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gruf/client/error_factory.rb', line 27

def initialize(
  default_class: nil,
  deserializer_class: nil,
  metadata_key: nil
)
  @default_class = default_class || Gruf::Client::Errors::Internal
  @metadata_key = ( || Gruf.).to_s
  default_serializer = if Gruf.error_serializer
                         Gruf.error_serializer.is_a?(Class) ? Gruf.error_serializer : Gruf.error_serializer.to_s.constantize
                       else
                         Gruf::Serializers::Errors::Json
                       end
  @deserializer_class = deserializer_class || default_serializer
end

Instance Method Details

#from_exception(exception) ⇒ Gruf::Client::Errors::Base|SignalException

Determine the proper error class to raise given the incoming exception. This will attempt to coalesce the exception object into the appropriate Gruf::Client::Errors subclass, or fallback to the default class if none is found (or it is a StandardError or higher-level error). It will leave alone Signals instead of attempting to coalesce them.

Parameters:

  • exception (Exception)

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gruf/client/error_factory.rb', line 51

def from_exception(exception)
  # passthrough on Signals, we don't want to mess with these
  return exception if exception.is_a?(SignalException)

  exception_class = determine_class(exception)
  if exception.is_a?(GRPC::BadStatus)
    # if it's a GRPC::BadStatus code, let's check for any trailing error metadata and decode it
    exception_class.new(deserialize(exception))
  else
    # otherwise, let's just capture the error and build the wrapper class
    exception_class.new(exception)
  end
end