Exception: Ja::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/ja/error.rb

Constant Summary collapse

ResponseError =

Base class for all errors

Class.new(Error)
ClientError =

Base class for errors in the 4xx range

Class.new(ResponseError)
ServerError =

Base class for errors in the 5xx range

Class.new(ResponseError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, uri, response) ⇒ Error

Returns a new instance of Error.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ja/error.rb', line 25

def initialize(verb, uri, response)
  @response = response
  @verb = verb
  @uri = uri

  @headline = "%{verb} %{url} responded with %{status}" % {
    status:  response.status,
    verb:    verb.to_s.upcase,
    url:     uri.to_s,
  }

  @response_body = Ja.format_body(response.headers) { response.body.to_s }

  @message = @response_body ? "#{@headline}\n\n#{@response_body}" : @headline
end

Instance Attribute Details

#headlineObject (readonly)

Returns the value of attribute headline.



41
42
43
# File 'lib/ja/error.rb', line 41

def headline
  @headline
end

#messageObject (readonly) Also known as: to_s

Returns the value of attribute message.



41
42
43
# File 'lib/ja/error.rb', line 41

def message
  @message
end

#responseObject (readonly)

Returns the value of attribute response.



23
24
25
# File 'lib/ja/error.rb', line 23

def response
  @response
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



41
42
43
# File 'lib/ja/error.rb', line 41

def response_body
  @response_body
end

#uriObject (readonly)

Returns the value of attribute uri.



23
24
25
# File 'lib/ja/error.rb', line 23

def uri
  @uri
end

#verbObject (readonly)

Returns the value of attribute verb.



23
24
25
# File 'lib/ja/error.rb', line 23

def verb
  @verb
end

Class Method Details

.fetch_error_class(status) ⇒ Object



19
20
21
# File 'lib/ja/error.rb', line 19

def self.fetch_error_class(status)
  const_get(HTTP::Response::Status::REASONS.fetch(status, "ResponseError").gsub(/\W/, ""))
end

.to_exception(verb, uri, response) ⇒ Object



15
16
17
# File 'lib/ja/error.rb', line 15

def self.to_exception(verb, uri, response)
  Error.fetch_error_class(response.status).new(verb, uri, response)
end

Instance Method Details

#statusObject



45
46
47
# File 'lib/ja/error.rb', line 45

def status
  response.status
end