appsignal-ruby
appsignal-ruby copied to clipboard
Don't log client-side errors in Grape middleware (e.g. validation errors)
When using the Appsignal::Integrations::Grape
middleware a lot of exceptions are logged to Appsignal that in our mind don't belong there. These include authorization errors, validation errors, or generally all kinds of client-side errors that would result in a 4xx HTTP status code.
We would rather have these errors logged for our client applications (such as frontends, or other APIs that happen to make incorrect API calls) than the service where the error is ultimately spotted.
https://github.com/appsignal/appsignal-ruby/blob/22b57fe2ec8ca78c72a83f5c05771c8cd7eb0cd1/lib/appsignal/integrations/grape.rb#L25-L28
There is a way to disable Grape error reporting, but I don't find it flexible enough, as I would have to know the error reason ahead of time, or perform some env
reconfiguring when raising an error.
We have reimplemented our middleware for this use case something like this:
rescue Exception => e # rubocop:disable Lint/RescueException
statuscode = e.try(:status) || 500
transaction.set_error(e) unless (400..499).include?(statuscode)
This makes use of the fact that Grape errors specifically all have an optional status
property that defines the response status (e.g. 400 for input validation errors). Our custom API errors also implement this field.
Would it make sense to add something like this to your provided middleware or how else would you recommend working around this issue, without having to implement/monkeypatch our own middleware?
Hey @padarom, we will discuss this and get back to you.