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 =
{
  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



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

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

Instance Attribute Details

#app_codeSymbol



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

def app_code
  @app_code
end

#codeSymbol



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

def code
  @code
end

#debug_infoObject

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



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

def debug_info
  @debug_info
end

#field_errorsArray



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

def field_errors
  @field_errors
end

#grpc_errorGRPC::BadStatus

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



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

def grpc_error
  @grpc_error
end

#messageString



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

def message
  @message
end

#metadataHash



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

def 
  
end

Instance Method Details

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

Add a field error to this error package



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

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



129
130
131
132
133
134
135
# File 'lib/gruf/error.rb', line 129

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.



144
145
146
# File 'lib/gruf/error.rb', line 144

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

#serializeString

Serialize the error for transport



118
119
120
121
# File 'lib/gruf/error.rb', line 118

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



100
101
102
# File 'lib/gruf/error.rb', line 100

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



153
154
155
156
157
158
159
160
161
# File 'lib/gruf/error.rb', line 153

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