ruby-grape / ruby-grape/grape

`endpoint_run.grape` callback is called without middlewares

Open
#2,010 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

Hello. I'm trying to add the monitoring by subscribing to 'endpoint_run.grape' via ActiveSupport::Notifications.subscribe.

It works well until an exception occurs inside API. I have a bunch of rescue_from blocks to return different status codes depending on exceptions, such as

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

The problem is when I call an endpoint which raises ActiveRecord::RecordNotFound, it really returns 404 status code, but in my callback data[:endpoint].status is still 200 (however data[:exception_object] is not null).

A quick look at the code shows, that callback wraps only the main call, without middlewares. I believe, it should wrap middlewares too because:

  • middlewares are part of the business logic. It's not good to have 404 returned to a client and 200 reported to the monitoring.
  • timing will be more accurate because the middlewares can eat time too.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue points to the endpoint_run.grape notification and the callback's wrapping of the main call versus middlewares; start by locating that notification and tracing middleware execution. Reproduce a rescue_from ActiveRecord::RecordNotFound request, then verify the callback observes the final 404 status and includes middleware time.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.