Exception: Herbert::Error::ApplicationError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/herbert/ApplicationError.rb

Overview

Error relevant in application context

Constant Summary collapse

Translation =

Code to text translation

{
  1000 => ["Malformated JSON", 400],
  1001 => ["Non-unicode encoding",400],
  1002 => ["Non-acceptable Accept header", 406],
  1003 => ["Not found", 404],
  1010 => ["Missing request body", 400],
  1011 => ["Missign required parameter", 400],
  1012 => ["Invalid request body", 400],
  1020 => ["Unspecified error occured", 500]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errno, http_code = nil, errors = []) ⇒ ApplicationError

Returns a new instance of ApplicationError.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
# File 'lib/herbert/ApplicationError.rb', line 22

def initialize(errno, http_code = nil, errors = [])
  raise ArgumentError, "Unknown error code: #{errno}" unless Translation.has_key?(errno.to_i)
  @code = errno.to_i
  @message = Translation[@code][0]
  @http_code = (http_code || Translation[@code][1])
  @errors = errors.to_a
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



8
9
10
# File 'lib/herbert/ApplicationError.rb', line 8

def code
  @code
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/herbert/ApplicationError.rb', line 8

def errors
  @errors
end

#http_codeObject (readonly)

Returns the value of attribute http_code.



8
9
10
# File 'lib/herbert/ApplicationError.rb', line 8

def http_code
  @http_code
end

#messageObject (readonly)

Returns the value of attribute message.



8
9
10
# File 'lib/herbert/ApplicationError.rb', line 8

def message
  @message
end

Class Method Details

.merge(errors) ⇒ Object

Add a hash of errors



40
41
42
43
44
45
46
# File 'lib/herbert/ApplicationError.rb', line 40

def self.merge(errors)
  if errors.is_a? Hash then
    Translation.merge!(errors)
  else
    raise ArgumentError("Expected a hash of codes and descriptions")
  end
end

.push(code, error) ⇒ Object

Add an error



35
36
37
# File 'lib/herbert/ApplicationError.rb', line 35

def self.push(code, error)
  Translation[code.to_s] = error.to_a
end

Instance Method Details

#to_hashObject



30
31
32
# File 'lib/herbert/ApplicationError.rb', line 30

def to_hash
  { :code => @code, :stackTrace => backtrace, :validationTrace => @errors}
end