Skip to content

Instantly share code, notes, and snippets.

@garmoshka-mo
Last active May 30, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garmoshka-mo/a5658eb4d875bd9acbd7a136340375b8 to your computer and use it in GitHub Desktop.
Save garmoshka-mo/a5658eb4d875bd9acbd7a136340375b8 to your computer and use it in GitHub Desktop.
Handling exceptions
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