grape icon indicating copy to clipboard operation
grape copied to clipboard

Add error suppressed by Grape::Middleware::Error to rack env(like action_dispatch.exception)

Open tevvi opened this issue 3 months ago • 2 comments

Rails add suppressed errors to rack envs, but i didnt found suppressed errors from grape in rack env So we wrote monkeypatch grape to access suppressed errors by Grape::Middleware::Error:

# grape 1.8.0 
module GrapeMiddlewareErrorPatch
  def run_rescue_handler(handler, e)
    @env['grape.exception'] = e
    super(handler, e)
  end
end

if defined?(Grape::Middleware::Error)
  Grape::Middleware::Error.prepend(GrapeMiddlewareErrorPatch)
end

So may be add this line to gem?)

tevvi avatar Sep 30 '25 14:09 tevvi

Would love some more info about what's going on here and why Rails does that, and yes please do open a PR?

dblock avatar Sep 30 '25 15:09 dblock

There we just add error to rack env to get access to this error from rack middlewares. For example we want to log input http requests? and we think it is will be helpful to log error happened in any framework. But usually frameworks suppress all errors to return always rack response [status, headers, body] But rails add suppressed error to rack env, so it is easy to access errors from rails: https://github.com/rails/rails/blob/529f933fc8b13114d308dd0752f76a9e293c8537/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L37

I will open MR for this line on 4-5 October

tevvi avatar Oct 01 '25 08:10 tevvi