Exception: X::HTTPError

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

Overview

Base class for HTTP errors from the X API

Direct Known Subclasses

ClientError, ServerError

Constant Summary collapse

JSON_CONTENT_TYPE_REGEXP =

Regular expression to match JSON content types

%r{application/(problem\+|)json}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:) ⇒ HTTPError

Initialize a new HTTPError

Examples:

Create an HTTP error

error = X::HTTPError.new(response: response)

Parameters:

  • response (Net::HTTPResponse)

    the HTTP response



32
33
34
35
36
# File 'lib/x/errors/http_error.rb', line 32

def initialize(response:)
  super(error_message(response))
  @response = response
  @code = response.code
end

Instance Attribute Details

#codeString (readonly)

The HTTP status code

Examples:

Get the status code

error.code

Returns:

  • (String)

    the HTTP status code



23
24
25
# File 'lib/x/errors/http_error.rb', line 23

def code
  @code
end

#responseNet::HTTPResponse (readonly)

The HTTP response

Examples:

Get the response

error.response

Returns:

  • (Net::HTTPResponse)

    the HTTP response



16
17
18
# File 'lib/x/errors/http_error.rb', line 16

def response
  @response
end

Instance Method Details

#error_message(response) ⇒ String

Get the error message from the response

Examples:

Get the error message

error.error_message(response)

Parameters:

  • response (Net::HTTPResponse)

    the HTTP response

Returns:

  • (String)

    the error message



45
46
47
48
49
50
51
# File 'lib/x/errors/http_error.rb', line 45

def error_message(response)
  if json?(response)
    message_from_json_response(response)
  else
    response.message
  end
end

#json?(response) ⇒ Boolean

Check if the response contains JSON

Examples:

Check if response is JSON

error.json?(response)

Parameters:

  • response (Net::HTTPResponse)

    the HTTP response

Returns:

  • (Boolean)

    true if the response is JSON



80
81
82
# File 'lib/x/errors/http_error.rb', line 80

def json?(response)
  JSON_CONTENT_TYPE_REGEXP === response["content-type"]
end

#message_from_json_response(response) ⇒ String

Extract error message from a JSON response

Examples:

Get error message from JSON

error.message_from_json_response(response)

Parameters:

  • response (Net::HTTPResponse)

    the HTTP response

Returns:

  • (String)

    the error message



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/x/errors/http_error.rb', line 60

def message_from_json_response(response)
  response_object = JSON.parse(response.body)
  if response_object["errors"].instance_of?(Array)
    response_object.fetch("errors").map { |error| error.fetch("message") }.join(", ")
  elsif response_object.key?("title") && response_object.key?("detail")
    "#{response_object.fetch("title")}: #{response_object.fetch("detail")}"
  elsif response_object.key?("error")
    response_object.fetch("error")
  else
    response.message
  end
end