Class: Gruf::Error

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gruf/error.rb

Overview

Represents a error that can be transformed into a gRPC error and have metadata attached to the trailing headers. This layer acts as an middle layer that can have metadata injection, tracing support, and other functionality not present in the gRPC core.

Constant Summary collapse

TYPES =

Returns A hash mapping of gRPC BadStatus codes to error symbols.

Returns:

  • (Hash<GRPC::BadStatus>)

    A hash mapping of gRPC BadStatus codes to error symbols

{
  ok: GRPC::Ok,
  cancelled: GRPC::Cancelled,
  unknown: GRPC::Unknown,
  invalid_argument: GRPC::InvalidArgument,
  bad_request: GRPC::InvalidArgument,
  deadline_exceeded: GRPC::DeadlineExceeded,
  not_found: GRPC::NotFound,
  already_exists: GRPC::AlreadyExists,
  unauthorized: GRPC::PermissionDenied,
  permission_denied: GRPC::PermissionDenied,
  unauthenticated: GRPC::Unauthenticated,
  resource_exhausted: GRPC::ResourceExhausted,
  failed_precondition: GRPC::FailedPrecondition,
  aborted: GRPC::Aborted,
  out_of_range: GRPC::OutOfRange,
  unimplemented: GRPC::Unimplemented,
  internal: GRPC::Internal,
  unavailable: GRPC::Unavailable,
  data_loss: GRPC::DataLoss
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(args = {}) ⇒ Error

Initialize the error, setting default values

Parameters:

  • args (Hash) (defaults to: {})

    (Optional) An optional hash of arguments that will set fields on the error object



74
75
76
77
78
79
# File 'lib/gruf/error.rb', line 74

def initialize(args = {})
  args.each do |k, v|
    send("#{k}=", v) if respond_to?(k)
  end
  @field_errors = []
end

Instance Attribute Details

#app_codeSymbol

Returns An arbitrary application code that can be used for logical processing of the error by the client.

Returns:

  • (Symbol)

    An arbitrary application code that can be used for logical processing of the error by the client



56
57
58
# File 'lib/gruf/error.rb', line 56

def app_code
  @app_code
end

#codeSymbol

Returns The given internal gRPC code for the error.

Returns:

  • (Symbol)

    The given internal gRPC code for the error



54
55
56
# File 'lib/gruf/error.rb', line 54

def code
  @code
end

#debug_infoObject

debug an given error response. This is sent by the server over the trailing metadata.

Returns:

  • (Object)

    A hash of debugging information, such as a stack trace and exception name, that can be used to



63
64
65
# File 'lib/gruf/error.rb', line 63

def debug_info
  @debug_info
end

#field_errorsArray

Returns An array of field errors that can be returned by the server.

Returns:

  • (Array)

    An array of field errors that can be returned by the server



60
61
62
# File 'lib/gruf/error.rb', line 60

def field_errors
  @field_errors
end

#grpc_errorGRPC::BadStatus

Return the appropriately mapped GRPC::BadStatus error object for this error

Returns:

  • (GRPC::BadStatus)


177
178
179
180
# File 'lib/gruf/error.rb', line 177

def grpc_error
  md = @metadata ? @metadata : {}
  @grpc_error = grpc_class.new(message, **md)
end

#messageString

Returns The error message returned by the server.

Returns:

  • (String)

    The error message returned by the server



58
59
60
# File 'lib/gruf/error.rb', line 58

def message
  @message
end

#metadataHash

Returns The trailing metadata that was attached to the error.

Returns:

  • (Hash)

    The trailing metadata that was attached to the error



67
68
69
# File 'lib/gruf/error.rb', line 67

def 
  @metadata
end

Instance Method Details

#add_field_error(field_name, error_code, message = '') ⇒ Object

Add a field error to this error package

Parameters:

  • field_name (Symbol)

    The field name for the error

  • error_code (Symbol)

    The application error code for the error; e.g. :job_not_found

  • message (String) (defaults to: '')

    The application error message for the error; e.g. “Job not found with ID 123”



88
89
90
# File 'lib/gruf/error.rb', line 88

def add_field_error(field_name, error_code, message = '')
  @field_errors << Errors::Field.new(field_name, error_code, message)
end

#attach_to_call(active_call) ⇒ Error

Append any appropriate errors to the gRPC call and properly update the output metadata

Parameters:

  • active_call (GRPC::ActiveCall)

    The marshalled gRPC call

Returns:

  • (Error)

    Return the error itself with the GRPC::ActiveCall attached and error metadata appended



138
139
140
141
142
143
144
# File 'lib/gruf/error.rb', line 138

def attach_to_call(active_call)
  [Gruf..to_sym] = serialize if Gruf.
  if !.empty? && active_call && active_call.respond_to?(:output_metadata)
    active_call..update()
  end
  self
end

#fail!(active_call) ⇒ GRPC::BadStatus

Fail the current gRPC call with the given error, properly attaching it to the call and raising the appropriate gRPC BadStatus code.

Parameters:

  • active_call (GRPC::ActiveCall)

    The marshalled gRPC call

Returns:

  • (GRPC::BadStatus)

    The gRPC BadStatus code this error is mapped to



153
154
155
# File 'lib/gruf/error.rb', line 153

def fail!(active_call)
  raise attach_to_call(active_call).grpc_error
end

#has_field_errors?Boolean

Return true if there are any present field errors

Returns:

  • (Boolean)

    True if the service has any field errors



97
98
99
# File 'lib/gruf/error.rb', line 97

def has_field_errors?
  @field_errors.any?
end

#serializeString

Serialize the error for transport

Returns:

  • (String)

    The serialized error message



127
128
129
130
# File 'lib/gruf/error.rb', line 127

def serialize
  serializer = serializer_class.new(self)
  serializer.serialize.to_s
end

#set_debug_info(detail, stack_trace = []) ⇒ Object

Set the debugging information for the error message

service

Parameters:

  • detail (String)

    The detailed message generated by the exception

  • stack_trace (Array<String>) (defaults to: [])

    An array of strings that represents the exception backtrace generated by the



108
109
110
# File 'lib/gruf/error.rb', line 108

def set_debug_info(detail, stack_trace = [])
  @debug_info = Errors::DebugInfo.new(detail, stack_trace)
end

#to_hHash

Return the error represented in Hash form

Returns:

  • (Hash)

    The error as a hash



162
163
164
165
166
167
168
169
170
# File 'lib/gruf/error.rb', line 162

def to_h
  {
    code: code,
    app_code: app_code,
    message: message,
    field_errors: field_errors.map(&:to_h),
    debug_info: debug_info.to_h
  }
end