Skip to content

Call #after of middleware on error#1240

Merged
dblock merged 2 commits into
ruby-grape:masterfrom
namusyaka:call-after-block-on-error
Jan 11, 2016
Merged

Call #after of middleware on error#1240
dblock merged 2 commits into
ruby-grape:masterfrom
namusyaka:call-after-block-on-error

Conversation

@namusyaka

Copy link
Copy Markdown
Contributor

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

Comment thread lib/grape/middleware/base.rb Outdated

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

dblock commented Jan 11, 2016

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor Author

Added.

@dblock

dblock commented Jan 11, 2016

Copy link
Copy Markdown
Member

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

dblock commented Jan 11, 2016

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Comment thread UPGRADING.md Outdated

Copy link
Copy Markdown
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
Copy Markdown
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

dblock commented Jan 11, 2016

Copy link
Copy Markdown
Member

Merged. Thanks!

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

Copy link
Copy Markdown
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

rngtng commented Feb 1, 2016

Copy link
Copy Markdown
Contributor

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

dblock commented Feb 1, 2016

Copy link
Copy Markdown
Member

@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

rngtng commented Feb 1, 2016

Copy link
Copy Markdown
Contributor

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