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

Helpers injected in rescue_from may conflict with middleware base methods #1451

Closed
dblock opened this issue Jul 20, 2016 · 3 comments
Closed

Comments

@dblock
Copy link
Member

dblock commented Jul 20, 2016

require 'spec_helper'

describe Grape::API do
  subject do
    Class.new(Grape::API) do
      format :json
      rescue_from :all do |e|
        rack_response({ message: e.message }, 400)
      end
      helpers do
        def error!(message, status, headers)
          raise 'this should never be called'
        end
      end
      rescue_from Grape::Exceptions::MethodNotAllowed do |e|
        error! e.message, e.status, e.headers
      end
      post do
        { ok: true }
      end
    end
  end

  def app
    subject
  end

  it '201 on post' do
    post '/'
    expect(last_response.status).to eq 201
  end

  it '405s on get' do
    get '/'
    expect(last_response.status).to eq 405
  end
end

In Grape 0.16.2 this passes.

In Grape 0.16.3 this fails with

Grape::API
  201 on post
  405s on get (FAILED - 1)

Failures:

  1) Grape::API 405s on get
     Failure/Error: raise 'this should never be called'

     RuntimeError:
       this should never be called
     # ./spec/grape/integration/error_in_middleware_spec.rb:12:in `error!'
     # ./spec/grape/integration/error_in_middleware_spec.rb:16:in `block (4 levels) in <top (required)>'
     # ./lib/grape/middleware/error.rb:76:in `instance_exec'
     # ./lib/grape/middleware/error.rb:76:in `exec_handler'
     # ./lib/grape/middleware/error.rb:45:in `rescue in call!'
     # ./lib/grape/middleware/error.rb:32:in `call!'
     # ./lib/grape/middleware/base.rb:23:in `call'
     # /Users/dblock/.rvm/gems/ruby-2.3.1/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
     # ./lib/grape/endpoint.rb:222:in `call!'
     # ./lib/grape/endpoint.rb:216:in `call'
     # ./lib/grape/router.rb:153:in `method_not_allowed'
     # ./lib/grape/router.rb:98:in `transaction'
     # ./lib/grape/router.rb:67:in `identity'
     # ./lib/grape/router.rb:52:in `block in call'
     # ./lib/grape/router.rb:115:in `with_optimization'
     # ./lib/grape/router.rb:51:in `call'
     # ./lib/grape/api.rb:119:in `call'
     # ./lib/grape/api.rb:45:in `call!'
     # ./lib/grape/api.rb:40:in `call'
     # /Users/dblock/.rvm/gems/ruby-2.3.1/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
     # /Users/dblock/.rvm/gems/ruby-2.3.1/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
     # /Users/dblock/.rvm/gems/ruby-2.3.1/gems/rack-test-0.6.3/lib/rack/test.rb:58:in `get'
     # ./spec/grape/integration/error_in_middleware_spec.rb:34:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Grape::Exceptions::MethodNotAllowed:
     #   405 Not Allowed
     #   ./lib/grape/router.rb:150:in `block (2 levels) in method_not_allowed'

Finished in 0.02251 seconds (files took 0.38461 seconds to load)
2 examples, 1 failure
@dblock
Copy link
Member Author

dblock commented Jul 20, 2016

This is caused by #1421, @namusyaka I think this is not a bug and by injecting helpers here we're doing the "right thing". Do let me know if you're thinking otherwise, I'll PR an UPGRADING text.

@namusyaka
Copy link
Contributor

I also think this is not a bug. Sorry for the inconvenience.

@dblock
Copy link
Member Author

dblock commented Jul 22, 2016

Closing with #1452 documenting this.

@dblock dblock closed this as completed Jul 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants