Exception: MixinBot::APIError

Inherits:
Error
  • Object
show all
Defined in:
lib/mixin_bot/errors.rb

Overview

Base class for Mixin API error responses with structured metadata.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, code: nil, description: nil, status: nil, http_status: nil, request_id: nil, server_time: nil, retry_after: nil, extra: nil, path: nil, verb: nil, body: nil) ⇒ APIError

rubocop:disable Metrics/ParameterLists – structured API error metadata



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mixin_bot/errors.rb', line 32

def initialize(message = nil, code: nil, description: nil, status: nil, http_status: nil,
               request_id: nil, server_time: nil, retry_after: nil, extra: nil,
               path: nil, verb: nil, body: nil)
  @code = code&.to_i
  @description = description
  @status = status&.to_i
  @http_status = http_status&.to_i
  @request_id = request_id
  @server_time = server_time
  @retry_after = retry_after
  @extra = extra
  @path = path
  @verb = verb
  @body = body
  super(message || formatted_message)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def code
  @code
end

#descriptionObject (readonly)

Returns the value of attribute description.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def description
  @description
end

#extraObject (readonly)

Returns the value of attribute extra.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def extra
  @extra
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def http_status
  @http_status
end

#pathObject (readonly)

Returns the value of attribute path.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def path
  @path
end

#request_idObject (readonly)

Returns the value of attribute request_id.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def request_id
  @request_id
end

#retry_afterObject (readonly)

Returns the value of attribute retry_after.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def retry_after
  @retry_after
end

#server_timeObject (readonly)

Returns the value of attribute server_time.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def server_time
  @server_time
end

#statusObject (readonly)

Returns the value of attribute status.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def status
  @status
end

#verbObject (readonly)

Returns the value of attribute verb.



28
29
30
# File 'lib/mixin_bot/errors.rb', line 28

def verb
  @verb
end

Class Method Details

.build(klass, verb:, path:, body:, response:, result:, code: nil, description: nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mixin_bot/errors.rb', line 81

def build(klass, verb:, path:, body:, response:, result:, code: nil, description: nil)
  err = result.is_a?(Hash) ? (result['error'] || {}) : {}
  resolved_code = code || err['code'] || infer_code_from_http(response)
  resolved_description = description || err['description'] || http_status_description(response&.status)
  headers = response&.headers || {}
  retry_after = headers['Retry-After'] if klass <= RateLimitError

  klass.new(
    code: resolved_code,
    description: resolved_description,
    status: err['status'],
    http_status: response&.status,
    request_id: headers['X-Request-Id'],
    server_time: headers['X-Server-Time'],
    retry_after: retry_after,
    extra: err['extra'],
    path: path,
    verb: verb,
    body: body
  )
end

Instance Method Details

#client_error?Boolean

rubocop:enable Metrics/ParameterLists

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/mixin_bot/errors.rb', line 50

def client_error?
  c = code.to_i
  return true if c.between?(400, 499)
  return true if http_status == 202 && c.positive? && c < 500

  false
end

#formatted_messageObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mixin_bot/errors.rb', line 66

def formatted_message
  format(
    '%<verb>s | %<path>s | %<body>s, errcode: %<code>s, errmsg: %<description>s, ' \
    'request_id: %<request_id>s, server_time: %<server_time>s',
    verb: verb.to_s.upcase,
    path: path.to_s,
    body: body.to_s,
    code: code,
    description: description,
    request_id: request_id,
    server_time: server_time
  )
end

#retryable?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mixin_bot/errors.rb', line 58

def retryable?
  false
end

#throttle?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/mixin_bot/errors.rb', line 62

def throttle?
  false
end