Class: PadlockAuth::Http::ErrorResponse
- Inherits:
-
Object
- Object
- PadlockAuth::Http::ErrorResponse
- Defined in:
- lib/padlock_auth/http/error_response.rb
Overview
A generic error response for PadlockAuth.
This class is intended to be extended by specific error responses.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
A human-readable description of the error.
-
#name ⇒ Symbol
readonly
The error name.
-
#status ⇒ Symbol
readonly
The HTTP status code.
Instance Method Summary collapse
-
#body ⇒ Hash
JSON response body.
-
#headers ⇒ Hash
HTTP headers.
-
#initialize(attributes = {}) ⇒ ErrorResponse
constructor
A new instance of ErrorResponse.
-
#raise_exception! ⇒ Object
Raise an exception based on the error response.
Constructor Details
#initialize(attributes = {}) ⇒ ErrorResponse
Returns a new instance of ErrorResponse.
13 14 15 16 |
# File 'lib/padlock_auth/http/error_response.rb', line 13 def initialize(attributes = {}) @name = attributes[:name] @status = attributes[:status] || :bad_request end |
Instance Attribute Details
#description ⇒ String (readonly)
Returns A human-readable description of the error.
26 27 28 29 30 31 32 |
# File 'lib/padlock_auth/http/error_response.rb', line 26 def description I18n.translate( name, scope: %i[padlock_auth errors messages], default: :server_error ) end |
#name ⇒ Symbol (readonly)
Returns The error name.
19 20 21 |
# File 'lib/padlock_auth/http/error_response.rb', line 19 def name @name end |
#status ⇒ Symbol (readonly)
Returns The HTTP status code.
22 23 24 |
# File 'lib/padlock_auth/http/error_response.rb', line 22 def status @status end |
Instance Method Details
#body ⇒ Hash
Returns JSON response body.
36 37 38 39 40 41 |
# File 'lib/padlock_auth/http/error_response.rb', line 36 def body { error: name, error_description: description }.reject { |_, v| v.blank? } end |
#headers ⇒ Hash
Returns HTTP headers.
45 46 47 48 49 50 51 |
# File 'lib/padlock_auth/http/error_response.rb', line 45 def headers { "Cache-Control" => "no-store, no-cache", "Content-Type" => "application/json; charset=utf-8", "WWW-Authenticate" => authenticate_info } end |
#raise_exception! ⇒ Object
Raise an exception based on the error response.
56 57 58 |
# File 'lib/padlock_auth/http/error_response.rb', line 56 def raise_exception! raise exception_class.new(self), description end |