Skip to content

Instantly share code, notes, and snippets.

@apostergiou
Created September 25, 2018 12:41
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 apostergiou/c7b31b95eb9f533ab6c1ea6cbf182b68 to your computer and use it in GitHub Desktop.
Save apostergiou/c7b31b95eb9f533ab6c1ea6cbf182b68 to your computer and use it in GitHub Desktop.
ruby print backtrace
rescue_from StandardError do |exception|
# Handle only JSON requests
raise unless 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], '']
.concat(err[:backtrace] || []).join "\n"
render :json => err, :status => 500
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment