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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/jamm/errors.rb', line 48

def initialize(args = {})
  # Check code existence and convert to string if it's integer
  @code = args[:code]
  @message = args[:message]
  @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.



36
37
38
# File 'lib/jamm/errors.rb', line 36

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



36
37
38
# File 'lib/jamm/errors.rb', line 36

def code
  @code
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



36
37
38
# File 'lib/jamm/errors.rb', line 36

def error_code
  @error_code
end

#headersObject (readonly)

Returns the value of attribute headers.



36
37
38
# File 'lib/jamm/errors.rb', line 36

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message.



36
37
38
# File 'lib/jamm/errors.rb', line 36

def message
  @message
end

Class Method Details

.from_error(e) ⇒ Object

Add this class method to convert StandardError to ApiError



39
40
41
42
43
44
45
46
# File 'lib/jamm/errors.rb', line 39

def self.from_error(e)
  new(
    code: e.code,
    message: e.message,
    response_headers: e.response_headers,
    response_body: e.response_body
  )
end

Instance Method Details

#to_sObject



115
116
117
118
# File 'lib/jamm/errors.rb', line 115

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