Skip to content

Instantly share code, notes, and snippets.

@ay589
Created September 25, 2018 12:41
Show Gist options
  • Save ay589/c7b31b95eb9f533ab6c1ea6cbf182b68 to your computer and use it in GitHub Desktop.
Save ay589/c7b31b95eb9f533ab6c1ea6cbf182b68 to your computer and use it in GitHub Desktop.

Revisions

  1. ay589 created this gist Sep 25, 2018.
    19 changes: 19 additions & 0 deletions backtrace.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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