Exception: Pendragon::Errors::Base

Inherits:
StandardError
  • Object
show all
Defined in:
lib/pendragon/errors.rb

Overview

Class for handling HTTP error.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers: {}, status: self.class.status, message: self.class.default_message, **payload) ⇒ Pendragon::Errors::Base

Constructs an instance of Errors::Base

Parameters:

  • [Hash{String (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options



50
51
52
53
54
55
# File 'lib/pendragon/errors.rb', line 50

def initialize(headers: {}, status: self.class.status, message: self.class.default_message, **payload)
  self.headers = self.class.default_headers.merge(headers)
  self.status, self.message = status, message
  parse_payload(**payload) if payload.kind_of?(Hash) && respond_to?(:parse_payload)
  super(message)
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/pendragon/errors.rb', line 8

def headers
  @headers
end

#messageObject

Returns the value of attribute message.



8
9
10
# File 'lib/pendragon/errors.rb', line 8

def message
  @message
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/pendragon/errors.rb', line 8

def status
  @status
end

Class Method Details

.create(**options, &block) ⇒ Class

Creates a new error class.

Examples:

require 'pendragon/errors'

BadRequest = Pendragon::Errors::Base.create(status: 400)

Parameters:

  • [Integer] (Hash)

    a customizable set of options

  • [Hash{String (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Class)


21
22
23
24
25
26
# File 'lib/pendragon/errors.rb', line 21

def self.create(**options, &block)
  Class.new(self) do
    options.each { |k, v| define_singleton_method(k) { v } }
    class_eval(&block) if block_given?
  end
end

.default_headersHash{String => String}

Returns default headers.

Returns:

  • (Hash{String => String})

    HTTP headers



39
40
41
# File 'lib/pendragon/errors.rb', line 39

def self.default_headers
  @default_headers ||= { 'Content-Type' => 'text/plain' }
end

.default_messageString

Returns default message.

Returns:

  • (String)

    default message for current status.

See Also:

  • [Rack[Rack::Utils[Rack::Utils::HTTP_STATUS_CODES]


32
33
34
# File 'lib/pendragon/errors.rb', line 32

def self.default_message
  @default_message ||= Rack::Utils::HTTP_STATUS_CODES.fetch(status, 'server error').downcase
end

Instance Method Details

#to_responseArray<Integer, Hash{String => String}, #each>

Converts self into response conformed Rack style.

Returns:

  • (Array<Integer, Hash{String => String}, #each>)

    response



60
61
62
# File 'lib/pendragon/errors.rb', line 60

def to_response
  [status, headers, [message]]
end