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

Can't rescue 415 Unsupported Media type error #1798

Open
apokyn opened this issue Oct 10, 2018 · 1 comment
Open

Can't rescue 415 Unsupported Media type error #1798

apokyn opened this issue Oct 10, 2018 · 1 comment
Labels

Comments

@apokyn
Copy link

apokyn commented Oct 10, 2018

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?

@dblock
Copy link
Member

dblock commented Oct 11, 2018

Care to try and write a spec that demonstrates what you're trying to do? I think everything should be rescuable but maybe at different levels.

@dblock dblock added the bug? label Oct 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants