Skip to content

Call #after of middleware on error #1240

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

Merged
merged 2 commits into from
Jan 11, 2016

Conversation

namusyaka
Copy link
Contributor

This might be able to close #1147.
Could you review this PR?

after_response = after
end

after_response || (error_class ? (fail error_class) : @app_response)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either all the paths aren't covered by specs or this can be:

begin
  @app_response = @app.call(@env)
ensure
  after || @app_response
end

Causes the after middleware to execute on error, and raises the exception as before.

What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, If after method returns rack response format on error, the response should be used forcibly, I think.
If not reasonable, I'm going to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the ensure section isn't used as retured value. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't be expecting the after block to return a response. Otherwise we need to change the behavior of before as well and this will definitely have impact on API implementations out there. These are callbacks, so I think they should be called, but their results discarded, at least as the next increment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.
So should I remove Response from this line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... wait. Looks like the old behavior was to use the response. So let me take this back, we should preserve the response if it was there. I guess it should be closer to this:

begin
  @app_response = @app.call(@env)
ensure
  @after_response = after
end
@after_response || @app_response

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks.

@namusyaka
Copy link
Contributor Author

I pushed new changes into this branch.
Currently, we raise the exception even if an exception is raised on @app.call.

If there is no problem, I'm going to squash a few commits and force push.

@dblock
Copy link
Member

dblock commented Jan 11, 2016

This needs a spec for the behavior of after returning a Rack response and overwriting the app response, so we don't break this in the future. Otherwise it's good to go.

@namusyaka
Copy link
Contributor Author

Added.

@dblock
Copy link
Member

dblock commented Jan 11, 2016

Forgot one more thing. The behavior is different from previous versions (even if it was undocumented), so this needs an entry in UPGRADING, please. Squash everything and I'll merge.

@dblock
Copy link
Member

dblock commented Jan 11, 2016

Thanks for hanging in with me!

@namusyaka namusyaka force-pushed the call-after-block-on-error branch from 45966fb to 06cdf05 Compare January 11, 2016 15:28
@namusyaka
Copy link
Contributor Author

Yeah, squashed and force pushed.
Thank you, too!

@namusyaka namusyaka force-pushed the call-after-block-on-error branch from 06cdf05 to bc6be4b Compare January 11, 2016 15:32
@@ -3,6 +3,30 @@ Upgrading Grape

### Upgrading to >= 0.14.0

#### Changes to behavior of `after` method of middleware on error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be under >= 0.15.0 and increment the version in version.rb to 0.15.0 as well, now that we have a major change.

Add a test for the bevhavior of returning Rack response by after callback
@namusyaka namusyaka force-pushed the call-after-block-on-error branch from bc6be4b to cc60281 Compare January 11, 2016 17:11
@namusyaka
Copy link
Contributor Author

Force pushed. Also I added a little Grape middleware section.

dblock added a commit that referenced this pull request Jan 11, 2016
@dblock dblock merged commit 76c900a into ruby-grape:master Jan 11, 2016
@dblock
Copy link
Member

dblock commented Jan 11, 2016

Merged. Thanks!

@namusyaka namusyaka deleted the call-after-block-on-error branch January 11, 2016 17:34
@namusyaka
Copy link
Contributor Author

Thank you for your polite response.

namusyaka added a commit to namusyaka/grape that referenced this pull request Jan 13, 2016
namusyaka added a commit to namusyaka/grape that referenced this pull request Jan 13, 2016
@rngtng
Copy link
Contributor

rngtng commented Feb 1, 2016

continue discussion from #1255: this change breaks https://github.com/aserafin/grape_logging (and all other middlewares which assumes @app_response isn't nil) on application error.

While updates on the middleware are pending, I'd propose to at least rescue any after call errors silently:

begin
  @app_response = @app.call(@env)
ensure
    after_response = after rescue nil
end

... or would setting @app_response to a default value a better solution?

@dblock
Copy link
Member

dblock commented Feb 1, 2016

@rngtng Could you open a new issue please? I really don't like hiding errors like this. Should we just take it all in and make grape_logging more resilient?

@rngtng
Copy link
Contributor

rngtng commented Feb 1, 2016

jepp, updating grape_logging is the right way to go, I only guess there are more middleware out there which probably break. Instead of hiding, we should communicate the error at least. (a warning?) - I'll open a issue for that...

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

Successfully merging this pull request may close these issues.

Call After block on error!
3 participants