Skip to content

Commit

Permalink
1. Refactor Controller exception handler to a separate method that ca…
Browse files Browse the repository at this point in the history
…n be overridden by controllers

2. Catch StandardErrors instead of Exceptions
  • Loading branch information
Pelle ten Cate committed Jan 15, 2019
1 parent 71db0be commit 5abc682
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/caprese/controller/concerns/errors.rb
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 5abc682

Please sign in to comment.