Class: Temescal::Error
- Inherits:
-
Object
- Object
- Temescal::Error
- Defined in:
- lib/temescal/error.rb
Constant Summary collapse
- NOT_FOUND_ERRORS =
["ActiveRecord::RecordNotFound", "Sinatra::NotFound"]
Instance Method Summary collapse
-
#formatted ⇒ Object
Public: Formats the exception for logging.
-
#ignore? ⇒ Boolean
Public: Determines whether an exception should be silenced.
-
#initialize(exception) ⇒ Error
constructor
Public: Instantiates a new Error.
-
#message ⇒ Object
Public: Determines whether to use a default message (as specified in the configuration) or the exception’s message for the API response.
-
#status ⇒ Object
Public: Determines the proper error code for the exception.
-
#type ⇒ Object
Public: Gets the exception’s type.
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
#formatted ⇒ Object
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 = "\n#{@exception.class}: #{@exception.}\n " << @exception.backtrace.join("\n ") << "\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.
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 |
#message ⇒ Object
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 configuration. || @exception. end |
#status ⇒ Object
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 |
#type ⇒ Object
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 |