Class: PadlockAuth::Http::ErrorResponse

Inherits:
Object
  • Object
show all
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

ForbiddenTokenResponse, InvalidTokenResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ErrorResponse

Returns a new instance of ErrorResponse.

Parameters:

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

    error attributes



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

#descriptionString (readonly)

Returns A human-readable description of the error.

Returns:

  • (String)

    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

#nameSymbol (readonly)

Returns The error name.

Returns:

  • (Symbol)

    The error name



19
20
21
# File 'lib/padlock_auth/http/error_response.rb', line 19

def name
  @name
end

#statusSymbol (readonly)

Returns The HTTP status code.

Returns:

  • (Symbol)

    The HTTP status code



22
23
24
# File 'lib/padlock_auth/http/error_response.rb', line 22

def status
  @status
end

Instance Method Details

#bodyHash

Returns JSON response body.

Returns:

  • (Hash)

    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

#headersHash

Returns HTTP headers.

Returns:

  • (Hash)

    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.

Raises:



56
57
58
# File 'lib/padlock_auth/http/error_response.rb', line 56

def raise_exception!
  raise exception_class.new(self), description
end