Exception: HttpResource::ApiError

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

Overview

Raised on a non-2xx response or a transport failure. Carries the HTTP status (an Integer, or nil for transport failures) + the raw body, so a background worker can branch its retry: drop on a 4xx (client_error?), retry on a 5xx (server_error?) or a transport failure (TransportError, status nil).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status: nil, body: nil) ⇒ ApiError

Returns a new instance of ApiError.



24
25
26
27
28
# File 'lib/http_resource/errors.rb', line 24

def initialize(message = nil, status: nil, body: nil)
  @status = status
  @body = body
  super(message || "HTTP error (status=#{status.inspect})")
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



22
23
24
# File 'lib/http_resource/errors.rb', line 22

def body
  @body
end

#statusObject (readonly)

Returns the value of attribute status.



22
23
24
# File 'lib/http_resource/errors.rb', line 22

def status
  @status
end

Class Method Details

.for_status(message, status:, body:) ⇒ Object

Map an HTTP status to the most specific ApiError subclass.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/http_resource/errors.rb', line 43

def self.for_status(message, status:, body:)
  klass =
    case status
    when 404 then NotFoundError
    when 422 then ValidationError
    when 401, 403 then AuthError
    when 400..499 then ClientError
    when 300..399 then RedirectError
    when 500..599 then ServerError
    else self
    end
  klass.new(message, status:, body:)
end

Instance Method Details

#client_error?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/http_resource/errors.rb', line 30

def client_error?
  status.is_a?(Integer) && status.between?(400, 499)
end

#not_found?Boolean

Returns:

  • (Boolean)


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

def not_found?
  status == 404
end

#server_error?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/http_resource/errors.rb', line 34

def server_error?
  status.is_a?(Integer) && status.between?(500, 599)
end