最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ruby on rails - When I have an error in a Grape::Api application, how can I always log it to Sentry? - Stack Overflow

matteradmin3PV0评论

We have a Ruby on Rails + Grape application with a lot going on. There are some places where we rescue exceptions and log messages or take corrective actions. Sometimes swallowing the exception, sometimes re-raising it.

We use Sentry as our APM. In some rescue's, we explicitly call Sentry.capture_exception(e) when we know we'll want to investigate after an error. For most "less interesting" rescues that are accepted normal operations, we don't log it to Sentry.

After yet-another uncaptured exception that we wish we could investigate, we decided to log all exceptions, even those that are, "not interesting."

What's the best way to log all rescued exception to Sentry (or Honeybadger, NewRelic, SignalFx, etc.) globally in a Rails app, and/or specifically a Grape app?

We have a Ruby on Rails + Grape application with a lot going on. There are some places where we rescue exceptions and log messages or take corrective actions. Sometimes swallowing the exception, sometimes re-raising it.

We use Sentry as our APM. In some rescue's, we explicitly call Sentry.capture_exception(e) when we know we'll want to investigate after an error. For most "less interesting" rescues that are accepted normal operations, we don't log it to Sentry.

After yet-another uncaptured exception that we wish we could investigate, we decided to log all exceptions, even those that are, "not interesting."

What's the best way to log all rescued exception to Sentry (or Honeybadger, NewRelic, SignalFx, etc.) globally in a Rails app, and/or specifically a Grape app?

Share Improve this question asked 7 hours ago David HempyDavid Hempy 6,2972 gold badges51 silver badges81 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I ended up adding a rescue_from callback to our base Grape-derived class:

class NiftyAPI < Grape::API
  rescue_from :all do |e|
    Rails.logger.error(e.message)
    Sentry.capture_exception(e)
    error!(e.message, e.status)
  end
end

Now, any rescued exception will get logged to Sentry.

If we find that overwhelming, we might blacklist select exception classes from getting sent to Sentry within that callback in the future. That's a more comfortable scenario that waiting to see which exceptions we explicitly want to call capture_exception on.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far