Exception: Fluence::Gateway::RequestError

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

Overview

Raised when the target service responded with a 4xx/5xx HTTP status.

Wraps the underlying OAuth2::Error but hides the OAuth2 dependency from callers and exposes status, body, and headers directly so consumers don't need to reach into error.response.*.

Two statuses get a subclass of their own, because they call for a different reaction than a plain 4xx/5xx: RateLimitedError (429) and UpstreamUnavailableError (502, 503, 504). Rescuing RequestError still catches both.

Examples:

Handle not-found specifically

begin
  Fluence::Gateway::Client.get('/api/v1/thing', service: :base_valeur)
rescue Fluence::Gateway::RequestError => e
  return nil if e.status == 404
  raise
end

Direct Known Subclasses

RateLimitedError, UpstreamUnavailableError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, response: nil) ⇒ RequestError

Builds a new RequestError wrapping the given message and optional response. Callers typically do not call this directly — the gateway client raises it automatically when the target service returns 4xx/5xx.

Parameters:

  • message (String) —

    human-readable error message

  • response (#status, #body, #headers, nil) (defaults to: nil) —

    the OAuth2::Response (or any duck-typed equivalent) that triggered the error

Instance Attribute Details

#body ⇒ String? (readonly)

Returns the raw response body of the failed response.

Returns:

  • (String, nil) —

    raw response body, or nil if no response was attached

#headers ⇒ Hash? (readonly)

Returns the response headers of the failed response.

Returns:

  • (Hash, nil) —

    response headers, or nil if no response was attached

#status ⇒ Integer? (readonly)

Returns the HTTP status code of the failed response.

Returns:

  • (Integer, nil) —

    HTTP status code, or nil if no response was attached

Class Method Details

.build(message, response:) ⇒ RequestError

Picks the error class matching the response status and instantiates it: Fluence::Gateway::RateLimitedError for 429, UpstreamUnavailableError for 502, 503 and 504, RequestError otherwise. A blank message is replaced by "HTTP <status>", so an empty error body (what a gateway sends when it could not reach the backend) still says which status came back.

Parameters:

  • message (String) —

    human-readable error message, usually OAuth2::Error#message

  • response (#status, #body, #headers, nil) —

    the response that triggered the error

Returns:

  • (RequestError) —

    an instance of the class matching the status