Exception: ForgetPasswords::ErrorResponse

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/forget-passwords.rb

Overview

An error response (with status, headers, etc) that can be raised and caught.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = nil, status = 500, headers = {}) ⇒ ErrorResponse

Create a new error response.

Parameters:

  • body (#to_s, #each) (defaults to: nil)

    the response body

  • status (Integer) (defaults to: 500)

    the HTTP status code

  • headers (Hash) (defaults to: {})

    the header set



31
32
33
34
35
36
37
# File 'lib/forget-passwords.rb', line 31

def initialize body = nil, status = 500, headers = {}
  if body.is_a? Rack::Response
    @response = body
  else
    @response = Rack::Response.new body, status, headers
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



23
24
25
# File 'lib/forget-passwords.rb', line 23

def response
  @response
end

Class Method Details

.exception(message) ⇒ ForgetPasswords::ErrorResponse

Generate a new exception with a Rack::Response as a message. Otherwise creates a new error response.

Parameters:

  • message (Rack::Response, #to_s)

    the response object or string

Returns:



63
64
65
66
67
68
69
70
# File 'lib/forget-passwords.rb', line 63

def self.exception message
  # XXX TODO auto generate (x)html from text message?
  case message
  when Rack::Response then self.new message
  else
    self.new message.to_s, 500, { 'Content-Type' => 'text/plain' }
  end
end

Instance Method Details

#exception(message = nil) ⇒ ForgetPasswords::ErrorResponse

Returns itself if the message is nil. Otherwise it runs the class method with the message as its argument.

Parameters:

  • message (nil, Rack::Response, #to_s) (defaults to: nil)

    optional response object or string

Returns:



80
81
82
83
# File 'lib/forget-passwords.rb', line 80

def exception message = nil
  return self if message.nil?
  self.class.exception message
end

#messageString

Returns the error message (which is the response body).

Returns:

  • (String)

    the error message (response body)



43
44
45
# File 'lib/forget-passwords.rb', line 43

def message
  @response.body
end

#message=(msg) ⇒ Object

Sets a new error message (response body). Does not change anything else, like headers or status or anything.

Parameters:

  • msg (#to_s)

    the new error message



52
53
54
# File 'lib/forget-passwords.rb', line 52

def message= msg
  @response.body = msg.to_s
end