Exception: Jamm::ApiError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/jamm/errors.rb

Overview

Purpose of this error handler is to normalize Jamm’s custom error and OpenAPI’s generated error, and enforce Jamm’s custom error format.

  • Jamm: code is string, message is string originating from Jamm’s protobuf definition.

  • OpenAPI: code is integer, message is string originating from ConnectRPC error format.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ApiError

Returns a new instance of ApiError.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jamm/errors.rb', line 65

def initialize(args = {})
  # Check code existence and convert to string if it's integer
  @code = args[:code]
  @message = args[:message]
  @error_type = args[:error_type]
  @headers = args[:response_headers]

  raw_body = if args[:response_body].nil?
               {}
             elsif args[:response_body].is_a?(Hash)
               args[:response_body]
             elsif args[:response_body].is_a?(String)
               begin
                 JSON.parse(args[:response_body])
               rescue StandardError
                 {}
               end
             else
               {}
             end

  @body = {}
  raw_body.each do |k, v|
    next if k == 'code'

    @body[k.to_sym] = v if k.respond_to?(:to_sym)
  end

  # Set human readable error type based on error code
  # https://github.com/connectrpc/connect-go/blob/d7c0966751650b41a9f1794513592e81b9beed45/code.go#L34
  @body[:error] = case raw_body['code']
                  when 1
                    'CANCELED'
                  when 2
                    'UNKNOWN'
                  when 3
                    'INVALID_ARGUMENT'
                  when 4
                    'DEADLINE_EXCEEDED'
                  when 5
                    'NOT_FOUND'
                  when 6
                    'ALREADY_EXISTS'
                  when 7
                    'PERMISSION_DENIED'
                  when 8
                    'RESOURCE_EXHAUSTED'
                  when 9
                    'FAILED_PRECONDITION'
                  when 10
                    'ABORTED'
                  when 11
                    'OUT_OF_RANGE'
                  when 12
                    'UNIMPLEMENTED'
                  when 13
                    'INTERNAL'
                  when 14
                    'UNAVAILABLE'
                  when 15
                    'DATA_LOSS'
                  when 16
                    'UNAUTHENTICATED'
                  end

  super(message)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



38
39
40
# File 'lib/jamm/errors.rb', line 38

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



38
39
40
# File 'lib/jamm/errors.rb', line 38

def code
  @code
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



38
39
40
# File 'lib/jamm/errors.rb', line 38

def error_code
  @error_code
end

#error_typeObject (readonly)

Returns the value of attribute error_type.



38
39
40
# File 'lib/jamm/errors.rb', line 38

def error_type
  @error_type
end

#headersObject (readonly)

Returns the value of attribute headers.



38
39
40
# File 'lib/jamm/errors.rb', line 38

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message.



38
39
40
# File 'lib/jamm/errors.rb', line 38

def message
  @message
end

Class Method Details

.from_error(e) ⇒ Object

Add this class method to convert StandardError to ApiError



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jamm/errors.rb', line 41

def self.from_error(e)
  error_type = Api::ErrorType::UNSPECIFIED

  # Convert Protobuf oriented error to error type.
  details = JSON.parse(e.response_body)['details']

  if details.is_a?(Array) && details.any?
    details.each do |d|
      # Loop through all error types to find a match
      Api::ErrorType.all_vars.each do |type|
        error_type = type.to_s if d['debug'] == type.to_s
      end
    end
  end

  new(
    code: e.code,
    message: e.message,
    error_type: error_type,
    response_headers: e.response_headers,
    response_body: e.response_body
  )
end

Instance Method Details

#to_sObject



133
134
135
136
# File 'lib/jamm/errors.rb', line 133

def to_s
  status_string = @code.nil? ? '' : "(Status #{@code}) "
  "#{status_string}#{@message}"
end