Class: HTTPX::ErrorResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
_Reader, ErrorResponsePatternMatchExtensions, Loggable, _Response
Defined in:
lib/httpx/response.rb,
sig/response.rbs

Overview

Wraps an error which has happened while processing an HTTP Request. It has partial public API parity with HTTPX::Response, so users should rely on it to infer whether the returned response is one or the other.

response = HTTPX.get("https://some-domain/path") #=> response is HTTPX::Response or HTTPX::ErrorResponse
response.raise_for_status #=> raises if it wraps an error

Constant Summary

Constants included from Loggable

Loggable::COLORS, Loggable::USE_DEBUG_LOG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorResponsePatternMatchExtensions

#deconstruct, #deconstruct_keys

Methods included from Loggable

#log, #log_exception, log_identifiers, #log_redact, #log_redact_body, #log_redact_headers

Constructor Details

#initialize(request, error) ⇒ ErrorResponse

Returns a new instance of ErrorResponse.

Parameters:



287
288
289
290
291
292
293
294
# File 'lib/httpx/response.rb', line 287

def initialize(request, error)
  @request = request
  @response = request.response if request.response.is_a?(Response)
  @error = error
  @options = request.options
  @request.log_exception(@error)
  finish!
end

Instance Attribute Details

#errorObject (readonly)

the wrapped exception.



279
280
281
# File 'lib/httpx/response.rb', line 279

def error
  @error
end

#requestObject (readonly)

the corresponding HTTPX::Request instance.



273
274
275
# File 'lib/httpx/response.rb', line 273

def request
  @request
end

#responseObject (readonly)

the HTTPX::Response instance, when there is one (i.e. error happens fetching the response).



276
277
278
# File 'lib/httpx/response.rb', line 276

def response
  @response
end

Instance Method Details

#<<(data) ⇒ Object

buffers lost chunks to error response



321
322
323
324
325
# File 'lib/httpx/response.rb', line 321

def <<(data)
  return unless @response

  @response << data
end

#closevoid

This method returns an undefined value.

closes the error resources.



302
303
304
# File 'lib/httpx/response.rb', line 302

def close
  @response.close if @response
end

#finish!Object



311
312
313
# File 'lib/httpx/response.rb', line 311

def finish!
  @request.connection = nil
end

#finished?Boolean

always true for error responses.

Returns:

  • (Boolean)


307
308
309
# File 'lib/httpx/response.rb', line 307

def finished?
  true
end

#peer_addressString, ...

Returns:

  • (String, IPAddr, nil)


99
# File 'sig/response.rbs', line 99

def peer_address: () -> (String | IPAddr)?

#raise_for_statusObject

raises the wrapped exception.

Raises:



316
317
318
# File 'lib/httpx/response.rb', line 316

def raise_for_status
  raise @error
end

#statusInteger, _ToS

Returns:

  • (Integer, _ToS)


95
# File 'sig/response.rbs', line 95

def status: () -> (Integer | _ToS)

#to_sObject

returns the exception full message.



297
298
299
# File 'lib/httpx/response.rb', line 297

def to_s
  @error.full_message(highlight: false)
end

#uriURI::Generic

Returns:

  • (URI::Generic)


97
# File 'sig/response.rbs', line 97

def uri: () -> URI::Generic