Exception: Forem::ForemError

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

Overview

Base error class for all errors raised by the forem-ruby library.

Every error exposes the raw HTTP context (status code, body, headers) so callers can inspect the upstream response without re-issuing the request.

Examples:

Rescuing a specific subclass

begin
  Forem::Article.retrieve(99999999)
rescue Forem::NotFoundError => e
  puts "#{e.http_status}: #{e.message}"
end

Rescuing any Forem error

rescue Forem::ForemError => e
  logger.error(e.message)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, http_status: nil, http_body: nil, http_headers: nil, code: nil) ⇒ ForemError

Initialize a new ForemError.

Parameters:

  • message (String, nil) (defaults to: nil)

    a human-readable description of the error.

  • http_status (Integer, nil) (defaults to: nil)

    the HTTP status code from the response.

  • http_body (String, nil) (defaults to: nil)

    the raw HTTP response body.

  • http_headers (Hash, nil) (defaults to: nil)

    a hash of HTTP response headers.

  • code (String, nil) (defaults to: nil)

    a machine-readable error code from the API.



41
42
43
44
45
46
47
# File 'lib/forem/errors.rb', line 41

def initialize(message = nil, http_status: nil, http_body: nil, http_headers: nil, code: nil)
  @http_status = http_status
  @http_body = http_body
  @http_headers = http_headers
  @code = code
  super(message)
end

Instance Attribute Details

#codeString? (readonly)

Returns an optional machine-readable error code extracted from the API response body, or nil.

Returns:

  • (String, nil)

    an optional machine-readable error code extracted from the API response body, or nil.



31
32
33
# File 'lib/forem/errors.rb', line 31

def code
  @code
end

#http_bodyString? (readonly)

Returns the raw HTTP response body as a string, or nil.

Returns:

  • (String, nil)

    the raw HTTP response body as a string, or nil.



24
25
26
# File 'lib/forem/errors.rb', line 24

def http_body
  @http_body
end

#http_headersHash? (readonly)

Returns a hash of HTTP response headers, or nil.

Returns:

  • (Hash, nil)

    a hash of HTTP response headers, or nil.



27
28
29
# File 'lib/forem/errors.rb', line 27

def http_headers
  @http_headers
end

#http_statusInteger? (readonly)

Returns the HTTP status code returned by the server (e.g. 401, 404, 429), or nil if no HTTP response was received.

Returns:

  • (Integer, nil)

    the HTTP status code returned by the server (e.g. 401, 404, 429), or nil if no HTTP response was received.



21
22
23
# File 'lib/forem/errors.rb', line 21

def http_status
  @http_status
end