Exception: EasyDocForms::APIError

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

Overview

An API-level failure: the request reached EasyDocForms and was rejected. Transport failures raise their ordinary Ruby exceptions instead.

Constant Summary collapse

PARTNER_API_NOT_ENABLED =

Machine-readable codes on authorization failures.

"PARTNER_API_NOT_ENABLED"
SCOPE_REQUIRED =
"SCOPE_REQUIRED"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status_code:, code: nil, hint: nil, completed_pdf_status: nil) ⇒ APIError

Returns a new instance of APIError.



19
20
21
22
23
24
25
# File 'lib/easydocforms/errors.rb', line 19

def initialize(message, status_code:, code: nil, hint: nil, completed_pdf_status: nil)
  super(message)
  @status_code = status_code
  @code = code
  @hint = hint
  @completed_pdf_status = completed_pdf_status
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



17
18
19
# File 'lib/easydocforms/errors.rb', line 17

def code
  @code
end

#completed_pdf_statusObject (readonly)

Returns the value of attribute completed_pdf_status.



17
18
19
# File 'lib/easydocforms/errors.rb', line 17

def completed_pdf_status
  @completed_pdf_status
end

#hintObject (readonly)

Returns the value of attribute hint.



17
18
19
# File 'lib/easydocforms/errors.rb', line 17

def hint
  @hint
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



17
18
19
# File 'lib/easydocforms/errors.rb', line 17

def status_code
  @status_code
end

Class Method Details

.from_response(status_code, body) ⇒ Object

Builds the right error subclass from an HTTP error response.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/easydocforms/errors.rb', line 28

def self.from_response(status_code, body)
  parsed = begin
    JSON.parse(body.to_s)
  rescue JSON::ParserError
    {}
  end
  parsed = {} unless parsed.is_a?(Hash)
  message = parsed["error"] || "HTTP #{status_code}"
  attrs = {
    status_code: status_code,
    code: parsed["code"],
    hint: parsed["hint"],
    completed_pdf_status: parsed["completed_pdf_status"]
  }
  klass =
    case status_code
    when 401 then AuthenticationError
    when 403 then PermissionError
    when 404 then NotFoundError
    when 409 then parsed["completed_pdf_status"] == "pending" ? PDFPendingError : APIError
    when 429 then RateLimitError
    else APIError
    end
  klass.new(message, **attrs)
end