lograge icon indicating copy to clipboard operation
lograge copied to clipboard

exception_object in not present in payload

Open mkarganov opened this issue 6 years ago • 7 comments

Hi, I am trying following from the Readme to have exception backtrace:

YourApp::Application.configure do
  config.lograge.enabled = true
  config.lograge.custom_options = lambda do |event|
    {
      exception: event.payload[:exception], # ["ExceptionClass", "the message"]
      exception_object: event.payload[:exception_object] # the exception instance
    }
  end
end

But once an error is raised exception_object is not present in a payload resulting to Could not log "process_action.action_controller" event. NoMethodError: undefined method "backtrace" for nil:NilClass.

Why it is not there and how I can get it?

mkarganov avatar Aug 28 '18 12:08 mkarganov

@mkarganov Did you find a solution for this? I am also facing the same issue. payload[:exception_object] is nil for me.

vik-y avatar Oct 08 '18 11:10 vik-y

@vik-y @mkarganov Where are the exceptions thrown? Do you have a stack trace?

pxlpnk avatar Oct 08 '18 15:10 pxlpnk

No, I have not solved it. I log errors now within a custom logger which has access to exception by handling them via rescue. @pxlpnk I would not try again, but if you will try the part from documentation that I posted and try to raise an exception via normal flow - you will find out that the proposed code does not work as event object is nil

mkarganov avatar Oct 08 '18 20:10 mkarganov

Exactly. I tried to test it by making a syntax error in my views and using the code provided in the documentation. I did get correct error message in payload[:exception] but payload[:exception_object] was nil.

vik-y avatar Oct 09 '18 11:10 vik-y

It shows up for me in a subset of events. When it does it doesn't have any backtrace... Rails 5.0 here.

kurko avatar Apr 12 '19 20:04 kurko

I am also facing this issue e.g inspection the payload I can see

:exception=>["ActiveRecord::RecordNotFound", "Couldn't find Upload with 'id'=318375"]

:exception_object is not set.

sgbett avatar Oct 01 '19 22:10 sgbett

looks like lib/lograge/log_subscribers/base.rb might be the place to fix it:

      def extract_status(payload)
        if (status = payload[:status])
          { status: status.to_i }
        elsif (error = payload[:exception])
          exception, message = error
          { status: get_error_status_code(exception), error: "#{exception}: #{message}" }
        else
          { status: default_status }
        end
      end

This also explains a weird discrepancy that I had somehow worked around some time ago, I have a

config.lograge.formatter = lambda do |data| ...

...block, in which I reference data[:error] to get the exception

sgbett avatar Oct 02 '19 00:10 sgbett