Class: MLB::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mlb/error_handler.rb

Overview

Handles HTTP error responses from the MLB Stats API

Constant Summary collapse

ERROR_MAP =

Mapping of HTTP status codes to error classes

{
  400 => BadRequest,
  401 => Unauthorized,
  403 => Forbidden,
  404 => NotFound,
  406 => NotAcceptable,
  409 => ConnectionException,
  410 => Gone,
  413 => PayloadTooLarge,
  422 => UnprocessableEntity,
  429 => TooManyRequests,
  500 => InternalServerError,
  502 => BadGateway,
  503 => ServiceUnavailable,
  504 => GatewayTimeout
}.freeze

Instance Method Summary collapse

Instance Method Details

#handle(response:) ⇒ String?

Handles an HTTP response

Examples:

handler.handle(response: response)

Parameters:

  • response (Net::HTTPResponse)

    the HTTP response

Returns:

  • (String, nil)

    the response body if successful

Raises:

  • (HTTPError)

    if the response is not successful



47
48
49
50
51
# File 'lib/mlb/error_handler.rb', line 47

def handle(response:)
  raise error(response) unless response.is_a?(Net::HTTPSuccess)

  response.body
end