class Gruf::Client::ErrorFactory
Translates exceptions into Gruf::Client::Errors
Public Class Methods
@param [Class] default_class @param [Class] deserializer_class @param [String|Symbol] metadata_key
# File lib/gruf/client/error_factory.rb, line 29 def initialize( default_class: nil, deserializer_class: nil, metadata_key: nil ) @default_class = default_class || Gruf::Client::Errors::Internal @metadata_key = (metadata_key || Gruf.error_metadata_key).to_s @deserializer_class = deserializer_class || default_serializer end
Public Instance Methods
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.
@param [Exception] exception @return [Gruf::Client::Errors::Base|SignalException]
# File lib/gruf/client/error_factory.rb, line 48 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
Private Instance Methods
@return [Gruf::Serializers::Errors::Base]
# File lib/gruf/client/error_factory.rb, line 98 def default_serializer return Gruf::Serializers::Errors::Json unless Gruf.error_serializer Gruf.error_serializer.is_a?(Class) ? Gruf.error_serializer : Gruf.error_serializer.to_s.constantize end
Deserialize any trailing metadata error payload from the exception
@param [Gruf::Client::Errors::Base] @return [String]
# File lib/gruf/client/error_factory.rb, line 70 def deserialize(exception) if exception.respond_to?(:metadata) key = exception.metadata.key?(@metadata_key.to_s) ? @metadata_key.to_s : @metadata_key.to_sym return @deserializer_class.new(exception.metadata[key]).deserialize if exception.metadata.key?(key) end exception end
@param [Exception] exception @return [Gruf::Client::Errors::Base]
# File lib/gruf/client/error_factory.rb, line 83 def determine_class(exception) error_class = Gruf::Client::Errors.const_get(exception.class.name.demodulize) # Ruby module inheritance will have StandardError, ScriptError, etc still get to this point # So we need to explicitly check for ancestry here return @default_class unless error_class.ancestors.include?(Gruf::Client::Errors::Base) error_class rescue NameError => _e @default_class end