Class: HTTPX::ErrorResponse

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

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_redact, #log_redact_body, #log_redact_headers

Constructor Details

#initialize(request, error) ⇒ ErrorResponse

Returns a new instance of ErrorResponse.



280
281
282
283
284
285
286
287
# File 'lib/httpx/response.rb', line 280

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

Instance Attribute Details

#errorObject (readonly)

the wrapped exception.



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

def error
  @error
end

#requestObject (readonly)

the corresponding HTTPX::Request instance.



266
267
268
# File 'lib/httpx/response.rb', line 266

def request
  @request
end

#responseObject (readonly)

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



269
270
271
# File 'lib/httpx/response.rb', line 269

def response
  @response
end

Instance Method Details

#<<(data) ⇒ Object

buffers lost chunks to error response



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

def <<(data)
  return unless @response

  @response << data
end

#closeObject

closes the error resources.



295
296
297
# File 'lib/httpx/response.rb', line 295

def close
  @response.close if @response
end

#finish!Object



304
305
306
# File 'lib/httpx/response.rb', line 304

def finish!
  @request.connection = nil
end

#finished?Boolean

always true for error responses.

Returns:

  • (Boolean)


300
301
302
# File 'lib/httpx/response.rb', line 300

def finished?
  true
end

#raise_for_statusObject

raises the wrapped exception.

Raises:



309
310
311
# File 'lib/httpx/response.rb', line 309

def raise_for_status
  raise @error
end

#to_sObject

returns the exception full message.



290
291
292
# File 'lib/httpx/response.rb', line 290

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