ruby-grape / ruby-grape/grape

Can't rescue 415 Unsupported Media type error

Open
#1,798 1 comment 0 reactions 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

Hi.

I have an issue with the grape that I cannot rescue 415 Unsupported Media type error. It does not appear in any rescue_from handlers, the reason for this is that this error is handled by grape with throw/ catch statements and logic for those different from raise/rescue.

I made a patch which catch thrown errors and than raises those thought grape base exception, like this:

module GrapeErrorPatch
  def call!(env)
    @env = env

    begin
      thrown_error = catch(:error) { return @app.call(@env) }

      raise Grape::Exceptions::Base.new(status: thrown_error[:status],
                                        message: thrown_error[:message])
    rescue Exception => error # rubocop:disable Lint/RescueException
      handler = select_error_handler(error)

      run_rescue_handler(handler, error)
    end
  end

  def select_error_handler(error)
    rescue_handler_for_base_only_class(error.class) ||
      rescue_handler_for_class_or_its_ancestor(error.class) ||
      rescue_handler_for_grape_exception(error.class) ||
      rescue_handler_for_any_class(error.class) ||
      raise
  end
end

::Grape::Middleware::Error.class_eval { prepend(GrapeErrorPatch) }

Could you please advice a better way to handle that? What is the grape way to handle thrown errors?

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

Start with Grape::Middleware::Error and the rescue_from handling path, then reproduce how a 415 Unsupported Media Type response is thrown. Determine the supported Grape mechanism for handling that error and verify that the resulting behavior is consistent with rescue_from expectations.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.