class Sanford::ErrorHandler
Constants
- STANDARD_ERROR_CLASSES
these are standard error classes that we rescue and run through any configured error procs; use the same standard error classes that dat-worker-pool rescues
Attributes
context[R]
error_procs[R]
exception[R]
Public Class Methods
new(exception, context_hash)
click to toggle source
# File lib/sanford/error_handler.rb, line 15 def initialize(exception, context_hash) @exception = exception @context = ErrorContext.new(context_hash) @error_procs = context_hash[:server_data].error_procs.reverse end
Public Instance Methods
run()
click to toggle source
The exception that we are generating a response for can change in the case that the configured error proc raises an exception. If this occurs, a response will be generated for that exception, instead of the original one. This is designed to avoid “hidden” errors happening, this way the server will respond and log based on the last exception that occurred.
# File lib/sanford/error_handler.rb, line 27 def run response = nil @error_procs.each do |error_proc| result = nil begin result = error_proc.call(@exception, @context) rescue *STANDARD_ERROR_CLASSES => proc_exception @exception = proc_exception end response ||= response_from_proc(result) end response || response_from_exception(@exception) end
Private Instance Methods
build_response(status, options = nil)
click to toggle source
# File lib/sanford/error_handler.rb, line 64 def build_response(status, options = nil) options ||= {} Sanford::Protocol::Response.new( [status, options[:message]], options[:data] ) end
response_from_exception(exception)
click to toggle source
# File lib/sanford/error_handler.rb, line 51 def response_from_exception(exception) if exception.kind_of?(Sanford::Protocol::BadMessageError) || exception.kind_of?(Sanford::Protocol::Request::InvalidError) build_response 400, :message => exception.message # BAD REQUEST elsif exception.kind_of?(Sanford::NotFoundError) build_response 404 # NOT FOUND elsif exception.kind_of?(Sanford::Protocol::TimeoutError) build_response 408 # TIMEOUT else build_response 500, :message => "An unexpected error occurred." # ERROR end end
response_from_proc(result)
click to toggle source
# File lib/sanford/error_handler.rb, line 43 def response_from_proc(result) if result.kind_of?(Sanford::Protocol::Response) result elsif result.kind_of?(Integer) || result.kind_of?(Symbol) build_response result end end