Exception: DHC::Error

Inherits:
StandardError
  • Object
show all
Includes:
FixInvalidEncodingConcern
Defined in:
lib/dhc/error.rb

Direct Known Subclasses

ClientError, ServerError, Timeout, UnknownError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, response) ⇒ Error

Returns a new instance of Error.



56
57
58
59
60
# File 'lib/dhc/error.rb', line 56

def initialize(message, response)
  super(message)
  self._message = message
  self.response = response
end

Instance Attribute Details

#_messageObject

Returns the value of attribute _message.



6
7
8
# File 'lib/dhc/error.rb', line 6

def _message
  @_message
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/dhc/error.rb', line 6

def response
  @response
end

Class Method Details

.dupObject



52
53
54
# File 'lib/dhc/error.rb', line 52

def self.dup
  self
end

.find(response) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/dhc/error.rb', line 44

def self.find(response)
  return DHC::Timeout if response.timeout?
  status_code = response.code.to_s[0..2].to_i
  error = map[status_code]
  error ||= DHC::UnknownError
  error
end

.mapObject



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
# File 'lib/dhc/error.rb', line 8

def self.map
  {
    400 => DHC::BadRequest,
    401 => DHC::Unauthorized,
    402 => DHC::PaymentRequired,
    403 => DHC::Forbidden,
    404 => DHC::NotFound,
    405 => DHC::MethodNotAllowed,
    406 => DHC::NotAcceptable,
    407 => DHC::ProxyAuthenticationRequired,
    408 => DHC::RequestTimeout,
    409 => DHC::Conflict,
    410 => DHC::Gone,
    411 => DHC::LengthRequired,
    412 => DHC::PreconditionFailed,
    413 => DHC::RequestEntityTooLarge,
    414 => DHC::RequestUriToLong,
    415 => DHC::UnsupportedMediaType,
    416 => DHC::RequestedRangeNotSatisfiable,
    417 => DHC::ExpectationFailed,
    422 => DHC::UnprocessableEntity,
    423 => DHC::Locked,
    424 => DHC::FailedDependency,
    426 => DHC::UpgradeRequired,

    500 => DHC::InternalServerError,
    501 => DHC::NotImplemented,
    502 => DHC::BadGateway,
    503 => DHC::ServiceUnavailable,
    504 => DHC::GatewayTimeout,
    505 => DHC::HttpVersionNotSupported,
    507 => DHC::InsufficientStorage,
    510 => DHC::NotExtended
  }
end

.to_aObject



62
63
64
# File 'lib/dhc/error.rb', line 62

def self.to_a
  [self]
end

Instance Method Details

#to_sObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dhc/error.rb', line 66

def to_s
  return response.to_s unless response.is_a?(DHC::Response)
  request = response.request
  return unless request.is_a?(DHC::Request)

  debug = []
  debug << [request.method, request.url].map { |str| self.class.fix_invalid_encoding(str) }.join(' ')
  debug << "Response Code: #{response.code} (#{response.options[:return_code]})"
  debug << response.body
  debug << _message

  debug.map { |str| self.class.fix_invalid_encoding(str) }.join("\n")
end