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

Call After block on error! #1147

Closed
ccastan1 opened this issue Sep 8, 2015 · 7 comments · Fixed by #1240
Closed

Call After block on error! #1147

ccastan1 opened this issue Sep 8, 2015 · 7 comments · Fixed by #1240

Comments

@ccastan1
Copy link

ccastan1 commented Sep 8, 2015

Not sure if this was resolved, but I've been trying to log api calls from the middleware and on an error! the after call doesn't get executed. Is there a good way around this issue? Thanks!

@dblock dblock added the bug? label Sep 9, 2015
@dblock
Copy link
Member

dblock commented Sep 9, 2015

You can write a separate middleware to handle this. I am not theoretically against to executing after blocks on error, but it would be a fairly major functionality change.

@dblock dblock added discuss! and removed bug? labels Sep 9, 2015
@dblock
Copy link
Member

dblock commented Sep 9, 2015

@ccastan1 It would be good to have a minimal example here or a spec that demonstrates this.

@frobichaud
Copy link

@ccastan1 @dblock, this is how I'm trying to wrap API calls with a middleware:

  require 'grape/middleware/base'

  class MyGrapeMiddleware < Grape::Middleware::Base
    def before
      env[:my_setting] = true
    end

    def after
      env[:my_setting] = false
    end
  end

Then in my API:

class MyApi < Grape::API
  use MyGrapeMiddleware
  resource :example do
    get do
      {}
    end
  end
end

My middleware after block is never called when the API endpoint raises an error...

@frobichaud
Copy link

Looks like the following middleware would do it:

require 'grape/middleware/base'

class MyGrapeMiddleware < Grape::Middleware::Base
  def before
    env[:my_setting] = true
  end

  def after
    env[:my_setting] = false
  end

  def call!(env)
    begin
      @app_response = super(env)
    ensure
      after
    end
  end
end

@dblock
Copy link
Member

dblock commented Sep 28, 2015

Makes sense @frobichaud. If you have time, see if any tests break if after is executed on error as well. If nothing breaks, I would take a PR that documents, tests and explains the change in UPGRADING.

@guizmaii
Copy link

👍 It could be a great functionality !

@rajnish4unow
Copy link

Great thanks for sharing this. I was trying to log on Success and Error so below implementation works for me.

  def before
    env[:request_json] = { start_time: Time.now }
  end

  def after
    json =  env[:request_json]
    json[:end_time] = Time.now
    puts json
  end

  rescue_from :all do |e|
    json =  env[:request_json]
    json[:end_time] = Time.now
    json[:errorMessage] = e.message
    puts json
  end

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

Successfully merging a pull request may close this issue.

5 participants