Class: Temescal::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/temescal/error.rb

Constant Summary collapse

NOT_FOUND_ERRORS =
["ActiveRecord::RecordNotFound", "Sinatra::NotFound"]

Instance Method Summary collapse

Constructor Details

#initialize(exception) ⇒ Error

Public: Instantiates a new Error.

exception - The raw exception being rescued.

Returns a new Error.



10
11
12
# File 'lib/temescal/error.rb', line 10

def initialize(exception)
  @exception = exception
end

Instance Method Details

#formattedObject

Public: Formats the exception for logging.

Returns a String containing the relevant exception information to log via STDERR.



36
37
38
39
40
# File 'lib/temescal/error.rb', line 36

def formatted
  message = "\n#{@exception.class}: #{@exception.message}\n  "
  message << @exception.backtrace.join("\n  ")
  message << "\n\n"
end

#ignore?Boolean

Public: Determines whether an exception should be silenced.

Returns true if the error type is configured as an ignored error, false otherwise.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/temescal/error.rb', line 53

def ignore?
  configuration.ignored_errors.each do |error|
    return true if @exception.is_a? error
  end

  false
end

#messageObject

Public: Determines whether to use a default message (as specified in the configuration) or the exception’s message for the API response.

Returns a String that’s either the default message or the exception’s message.



19
20
21
# File 'lib/temescal/error.rb', line 19

def message
  configuration.default_message || @exception.message
end

#statusObject

Public: Determines the proper error code for the exception.

Returns a Fixnum based on the exception’s type and http_status attribute (if applicable), will return a generic 500 for all others.



27
28
29
30
# File 'lib/temescal/error.rb', line 27

def status
  return 404 if NOT_FOUND_ERRORS.include? @exception.class.to_s
  @exception.respond_to?(:http_status) ? @exception.http_status : 500
end

#typeObject

Public: Gets the exception’s type.

Returns a String representing the exception class name.



45
46
47
# File 'lib/temescal/error.rb', line 45

def type
  @exception.class.to_s
end