Last active
May 30, 2018 14:30
-
-
Save garmoshka-mo/a5658eb4d875bd9acbd7a136340375b8 to your computer and use it in GitHub Desktop.
Handling exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rescue_from StandardError do |exception| | |
if request.xhr? | |
render text: exception.message, :status => 500 | |
elsif request.format.json? | |
err = {error: exception.message} | |
err[:backtrace] = exception.backtrace.select do |line| | |
# filter out non-significant lines: | |
%w(/gems/ /rubygems/ /lib/ruby/).all? do |litter| | |
not line.include?(litter) | |
end | |
end if Rails.env.development? and exception.is_a? Exception | |
# duplicate exception output to console: | |
STDERR.puts ["⛔️ Error:", | |
err[:error], | |
'', | |
err[:backtrace] || []].join "\n" | |
render json: err, status: 500 | |
else | |
raise | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment