Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic error handling #875

Closed
justin808 opened this issue Jun 9, 2019 · 4 comments · Fixed by #919
Closed

Generic error handling #875

justin808 opened this issue Jun 9, 2019 · 4 comments · Fixed by #919
Assignees

Comments

@justin808
Copy link

My gem https://github.com/shakacode/react_on_rails supports HoneyBadger and Sentry with this code:

https://github.com/shakacode/react_on_rails/blob/master/lib/react_on_rails/prerender_error.rb#L25

How do I support Rollbar in a similar manner?

Thanks,

Justin

module ReactOnRails
  class PrerenderError < ::ReactOnRails::Error
    MAX_ERROR_SNIPPET_TO_LOG = 1000
    # TODO: Consider remove providing original `err` as already have access to `self.cause`
    # http://blog.honeybadger.io/nested-errors-in-ruby-with-exception-cause/
    attr_reader :component_name, :err, :props, :js_code, :console_messages

    # err might be nil if JS caught the error
    def initialize(component_name: nil, err: nil, props: nil,
                   js_code: nil, console_messages: nil)
      @component_name = component_name
      @err = err
      @props = props
      @js_code = js_code
      @console_messages = console_messages

      backtrace, message = calc_message(component_name, console_messages, err, js_code, props)

      super([message, backtrace].compact.join("\n"))
    end

    def to_honeybadger_context
      to_error_context
    end

    def raven_context
      to_error_context
    end

    def to_error_context
      result = {
        component_name: component_name,
        err: err,
        props: props,
        js_code: js_code,
        console_messages: console_messages
      }

      result.merge!(err.to_error_context) if err.respond_to?(:to_error_context)
      result
    end
@waltjones
Copy link
Contributor

@justin808 Can you point to where this is integrating with Raven and Honeybadger? I'm not sure what the rollbar-gem analog is, since I don't know how the existing examples get consumed.

@justin808
Copy link
Author

@waltjones

Honeybadger link

When the #to_honeybadger_context method is defined on an Exception class, the context will be automatically added when the exception is reported:

class CustomError < StandardError
  def to_honeybadger_context
    { tags: 'custom' }
  end
end

raise CustomError, 'This error will be reported with context'

Sentry, Raven:
https://github.com/getsentry/raven-ruby/search?q=raven_context&unscoped_q=raven_context

https://github.com/getsentry/raven-ruby/blob/284e39422068b3f3b1b1223a5b43dd6c756c9397/lib/raven/event.rb#L51

@justin808
Copy link
Author

Is this a good workaround:

  config.custom_data_method = lambda do |_message, exception, _context|
    # Need to find the first exception in the cause chain that responds to "to_error_context"
    # Normal catch of the React on Rails error is wrapped in ActionView::Template::Error
    error = exception
    error = error.cause while error && !error.respond_to?(:to_error_context)

    if error&.respond_to?(:to_error_context)
      {
        ror_error_context: error.to_error_context,
      }
    end
  end

@waltjones
Copy link
Contributor

@justin808 Thanks I see now. Yes, that's the most correct way based on what rollbar-gem currently supports. Supporting error.rollbar_context seems like a neat idea in the future though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants