Exception: X::InvalidResponse

Inherits:
HTTPError show all
Defined in:
x-core/lib/x/core/errors/invalid_response.rb

Overview

Error raised for a successful response whose body is not JSON, such as the page of a proxy or captive portal

It is an X::HTTPError, so that code that rescues the failures of a response reads it as it reads any other: the status is HTTPError#status, the headers are HTTPError#headers, the body is #body, and HTTPError#http_method and HTTPError#uri are the request it answered. A body that is not JSON describes no problem, so HTTPError#problem is nil and HTTPError#problems empty.

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, http_response: nil, status: nil, headers: nil, body: nil, http_method: nil, uri: nil) ⇒ InvalidResponse

Initialize a new InvalidResponse

Public, so that code that rescues an InvalidResponse can be tested with one built from the status, headers, and body of a response, or from a Net::HTTP response, as ResponseParser and the stream of x-streaming build one. The error names the request, when given its method and URI, as x-core names the request the response answers.

It can be raised as any other exception is, as in raise X::InvalidResponse, and is built with a status of 200 when it is given neither a response nor a status, as HTTPError states.

Examples:

Create the error of a page a proxy answered with

error = X::InvalidResponse.new(status: 200, headers: {"content-type" => "text/html"}, body: "<html></html>")

Create an error for a line of a stream

error = X::InvalidResponse.new(http_response: response, body: line, http_method: :get, uri: stream_uri)

Parameters:

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

    the message, or nil for the one that names the status and content type

  • http_response (Net::HTTPResponse, nil) (defaults to: nil) —

    the HTTP response, or nil for one built of the status, headers, and body

  • status (Integer, nil) (defaults to: nil) —

    the status of the response, from 100 to 599, when it is not given, or nil for 200

  • headers (Hash{String => String}, nil) (defaults to: nil) —

    the headers of the response, when it is not given

  • body (String, nil) (defaults to: nil) —

    the body that is not JSON, which is the body of a response built of the status

  • http_method (Symbol, String, nil) (defaults to: nil) —

    the method of the request the response answers, in any case

  • uri (URI::Generic, nil) (defaults to: nil) —

    the URI of the request the response answers

Raises:

  • (ArgumentError) —

    if the HTTP response is given beside a status or headers, or the status is not from 100 to 599, or the headers are not a Hash of names to values



56
57
58
59
# File 'x-core/lib/x/core/errors/invalid_response.rb', line 56

def initialize(message = nil, http_response: nil, status: nil, headers: nil, body: nil, http_method: nil, uri: nil)
  @body = body
  super(message, http_response:, status:, headers:, body: (body if http_response.nil?), http_method:, uri:)
end

Instance Method Details

#body ⇒ String?

The body that is not JSON: the whole body of a response, or the line of a stream

The body of a stream can be read only as it arrives, so an error raised for a line of a stream holds that line. It is tagged UTF-8, as Response#body is, and keeps the bytes of a body that is not valid UTF-8.

An error given no body holds the body of its response, as HTTPError#body does, once that response has been read whole. The body of a response that has not been read, as that of a stream still arriving, is never read for it, since reading it would wait for the rest of the stream, or take the lines the stream has yet to read.

Examples:

Read the body that could not be parsed

error.body

Returns:

  • (String, nil) —

    the body, or the line of a stream, tagged UTF-8, or else the body of the response once it has been read whole, or nil for an error of a response that has not been, or that has none



29
# File 'x-core/lib/x/core/errors/invalid_response.rb', line 29

def body = @body || body_read