Skip to content

Commit

Permalink
Merge pull request #52 from nicklandgrebe/exception_handler_method
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
nicklandgrebe committed Jan 30, 2019
2 parents 71db0be + 5abc682 commit 1cbdb50
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/caprese/controller/concerns/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,10 @@ module Errors

included do
around_action :enable_caprese_style_errors

rescue_from Exception do |e|
Caprese::Record.caprese_style_errors = false

if e.is_a?(Caprese::Error)
output = { json: e }
render output.merge(e.header)
else
logger.info e.inspect
logger.info e.backtrace.join("\n")
render json: Caprese::Error.new(code: :server_error), status: 500
end
end
rescue_from(StandardError) { |e| handle_exception(e) }
end


# Fail with a controller action error
#
# @param [Symbol] field the field (a controller param) that caused the error
Expand All @@ -46,5 +35,19 @@ def enable_caprese_style_errors
yield
Caprese::Record.caprese_style_errors = false
end

# Gracefully handles exceptions raised during Caprese controller actions.
# Override this method in your controller to add your own exception handlers
def handle_exception(e)
Caprese::Record.caprese_style_errors = false
if e.is_a?(Caprese::Error)
output = { json: e }
render output.merge(e.header)
else
logger.info e.inspect
logger.info e.backtrace.join("\n")
render json: Caprese::Error.new(code: :server_error), status: 500
end
end
end
end

0 comments on commit 1cbdb50

Please sign in to comment.