Skip to content

Grape logger always log 500 http response code for rescued exceptions #10

@nbulaj

Description

@nbulaj

Hi. While exploring the grape-middleware-logger logs I discovered that all requests with errors that are preprocessed with rescue_from are marked with 500 response code:

# api.rb
class API < Grape::API
  use Grape::Middleware::Logger

  format :json

  include ExceptionHandler

  mount V1::Base
end

# exception_handler.rb
module ExceptionHandler
  extend ActiveSupport::Concern

  included do
    rescue_from ActiveRecord::RecordNotFound do
      error!({ error: :not_found }, 404)
    end

    # ... other :rescue_from ...
  end
end

Log:

I, [2016-09-14T14:53:49.896641 #27381] INFO -- : [f4b3b020-ef4b-44ea-9228-fc58d35c1687] ActiveRecord::RecordNotFound (Couldn't find User with 'id'=1010):

I looked at the source code of the gem and added some trace to the code. And what am I found is that rescue_from are invoked after grape-middleware-logger methods such as after_exception and after_failure. So the real status code (and maybe some extra info) does not fall into the log, only 500. I think that is a wrong behaviour.

Gemfile.lock

grape (0.17.0)
grape-middleware-logger (1.7.0)

What do you think about that, @ridiculous?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions