Module: Excon::Errors

Defined in:
lib/excon/errors.rb

Defined Under Namespace

Classes: Accepted, BadGateway, BadRequest, ClientError, Conflict, Continue, Created, Error, ExpectationFailed, Forbidden, Found, GatewayTimeout, Gone, HTTPStatusError, Informational, InternalServerError, LengthRequired, MethodNotAllowed, MovedPermanently, MultipleChoices, NoContent, NonAuthoritativeInformation, NotAcceptable, NotFound, NotImplemented, NotModified, OK, PartialContent, PaymentRequired, PreconditionFailed, ProxyAuthenticationRequired, ProxyConnectionError, ProxyParseError, Redirection, RequestEntityTooLarge, RequestTimeout, RequestURITooLong, RequestedRangeNotSatisfiable, ResetContent, ResponseParseError, SeeOther, ServerError, ServiceUnavailable, SocketError, StubNotFound, Success, SwitchingProtocols, TemporaryRedirect, Timeout, Unauthorized, UnprocessableEntity, UnsupportedMediaType, UseProxy

Class Method Summary collapse

Class Method Details

.status_error(request, response) ⇒ Object

Messages for nicer exceptions, from rfc2616



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/excon/errors.rb', line 88

def self.status_error(request, response)
  @errors ||= {
    100 => [Excon::Errors::Continue, 'Continue'],
    101 => [Excon::Errors::SwitchingProtocols, 'Switching Protocols'],
    200 => [Excon::Errors::OK, 'OK'],
    201 => [Excon::Errors::Created, 'Created'],
    202 => [Excon::Errors::Accepted, 'Accepted'],
    203 => [Excon::Errors::NonAuthoritativeInformation, 'Non-Authoritative Information'],
    204 => [Excon::Errors::NoContent, 'No Content'],
    205 => [Excon::Errors::ResetContent, 'Reset Content'],
    206 => [Excon::Errors::PartialContent, 'Partial Content'],
    300 => [Excon::Errors::MultipleChoices, 'Multiple Choices'],
    301 => [Excon::Errors::MovedPermanently, 'Moved Permanently'],
    302 => [Excon::Errors::Found, 'Found'],
    303 => [Excon::Errors::SeeOther, 'See Other'],
    304 => [Excon::Errors::NotModified, 'Not Modified'],
    305 => [Excon::Errors::UseProxy, 'Use Proxy'],
    307 => [Excon::Errors::TemporaryRedirect, 'Temporary Redirect'],
    400 => [Excon::Errors::BadRequest, 'Bad Request'],
    401 => [Excon::Errors::Unauthorized, 'Unauthorized'],
    402 => [Excon::Errors::PaymentRequired, 'Payment Required'],
    403 => [Excon::Errors::Forbidden, 'Forbidden'],
    404 => [Excon::Errors::NotFound, 'Not Found'],
    405 => [Excon::Errors::MethodNotAllowed, 'Method Not Allowed'],
    406 => [Excon::Errors::NotAcceptable, 'Not Acceptable'],
    407 => [Excon::Errors::ProxyAuthenticationRequired, 'Proxy Authentication Required'],
    408 => [Excon::Errors::RequestTimeout, 'Request Timeout'],
    409 => [Excon::Errors::Conflict, 'Conflict'],
    410 => [Excon::Errors::Gone, 'Gone'],
    411 => [Excon::Errors::LengthRequired, 'Length Required'],
    412 => [Excon::Errors::PreconditionFailed, 'Precondition Failed'],
    413 => [Excon::Errors::RequestEntityTooLarge, 'Request Entity Too Large'],
    414 => [Excon::Errors::RequestURITooLong, 'Request-URI Too Long'],
    415 => [Excon::Errors::UnsupportedMediaType, 'Unsupported Media Type'],
    416 => [Excon::Errors::RequestedRangeNotSatisfiable, 'Request Range Not Satisfiable'],
    417 => [Excon::Errors::ExpectationFailed, 'Expectation Failed'],
    422 => [Excon::Errors::UnprocessableEntity, 'Unprocessable Entity'],
    500 => [Excon::Errors::InternalServerError, 'InternalServerError'],
    501 => [Excon::Errors::NotImplemented, 'Not Implemented'],
    502 => [Excon::Errors::BadGateway, 'Bad Gateway'],
    503 => [Excon::Errors::ServiceUnavailable, 'Service Unavailable'],
    504 => [Excon::Errors::GatewayTimeout, 'Gateway Timeout']
  }

  error, message = @errors[response[:status]] || [Excon::Errors::HTTPStatusError, 'Unknown']

  message = "Expected(#{request[:expects].inspect}) <=> Actual(#{response[:status]} #{message})"

  if request[:debug_request]
    # scrub authorization
    req = request.dup
    req.reject! {|key, value| [:connection, :stack].include?(key)}
    if req.has_key?(:headers) && req[:headers].has_key?('Authorization')
      req[:headers] = req[:headers].dup
      req[:headers]['Authorization'] = REDACTED
    end
    message << "\n  request => #{req.inspect}"
  end

  message << "\n  response => #{response.inspect}" if request[:debug_response]

  error.new(message, request, response)
end