1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
# frozen_string_literal: true
module HTTPX
# the default exception class for exceptions raised by HTTPX.
class Error < StandardError; end
class UnsupportedSchemeError < Error; end
class ConnectionError < Error; end
# Error raised when there was a timeout. Its subclasses allow for finer-grained
# control of which timeout happened.
class TimeoutError < Error
# The timeout value which caused this error to be raised.
attr_reader :timeout
# initializes the timeout exception with the +timeout+ causing the error, and the
# error +message+ for it.
def initialize(timeout, message)
@timeout = timeout
super(message)
end
# clones this error into a HTTPX::ConnectionTimeoutError.
def to_connection_error
ex = ConnectTimeoutError.new(@timeout, message)
ex.set_backtrace(backtrace)
ex
end
end
# Raise when it can't acquire a connection from the pool.
class PoolTimeoutError < TimeoutError; end
# Error raised when there was a timeout establishing the connection to a server.
# This may be raised due to timeouts during TCP and TLS (when applicable) connection
# establishment.
class ConnectTimeoutError < TimeoutError; end
# Error raised when there was a timeout while sending a request, or receiving a response
# from the server.
class RequestTimeoutError < TimeoutError
# The HTTPX::Request request object this exception refers to.
attr_reader :request
# initializes the exception with the +request+ and +response+ it refers to, and the
# +timeout+ causing the error, and the
def initialize(request, response, timeout)
@request = request
@response = response
super(timeout, "Timed out after #{timeout} seconds")
end
def marshal_dump
[message]
end
end
# Error raised when there was a timeout while receiving a response from the server.
class ReadTimeoutError < RequestTimeoutError; end
# Error raised when there was a timeout while sending a request from the server.
class WriteTimeoutError < RequestTimeoutError; end
# Error raised when there was a timeout while waiting for the HTTP/2 settings frame from the server.
class SettingsTimeoutError < TimeoutError; end
# Error raised when there was a timeout while resolving a domain to an IP.
class ResolveTimeoutError < TimeoutError; end
# Error raise when there was a timeout waiting for readiness of the socket the request is related to.
class OperationTimeoutError < TimeoutError; end
# Error raised when there was an error while resolving a domain to an IP.
class ResolveError < Error; end
# Error raised when there was an error while resolving a domain to an IP
# using a HTTPX::Resolver::Native resolver.
class NativeResolveError < ResolveError
attr_reader :host
attr_accessor :connection
# initializes the exception with the +connection+ it refers to, the +host+ domain
# which failed to resolve, and the error +message+.
def initialize(connection, host, message = "Can't resolve #{host}")
@connection = connection
@host = host
super(message)
end
end
# The exception class for HTTP responses with 4xx or 5xx status.
class HTTPError < Error
# The HTTPX::Response response object this exception refers to.
attr_reader :response
# Creates the instance and assigns the HTTPX::Response +response+.
def initialize(response)
@response = response
super("HTTP Error: #{@response.status} #{@response.headers}\n#{@response.body}")
end
# The HTTP response status.
#
# error.status #=> 404
def status
@response.status
end
end
end
|