-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
error! method returns wrong content type "text/html" for 404 status code #1515
Comments
Looking at the HTML I think this is caught by Sinatra. I would dig there next. Maybe try to build a quick repro? We could also use something similar on top of sinatra to https://github.com/ruby-grape/grape-on-rack and https://github.com/ruby-grape/grape-on-rails, so if you make a basic grape-on-sinatra implementation we can move it into the ruby-grape org and use it as a place to debug similar things. Maybe based on https://github.com/katgironpe/sinatra-pg-grape (without a database). |
Hi @khelll. It is definitely the problem of Sinatra gem, not Grape. So take a look one again at your routes and rescuers. Without some code gist it is hard to understand what is going on. |
Closing this, a repro would be a start if you need help @khelll. |
I ran into this today... It indeed is a setup issue. It is highly dependent on how you set up integrating grape and sinatra. For myself, I was able to fix this by changing the order of loading grape and sinatra in config.ru: From:
to:
This caused the 404 to be intercepted by sinatra but be the last and final output in the middleware chain. Hope this helps someone else in this situation if you're using the same rack setup as mine! |
Want to write an integration test for this @jonmchan? |
@dblock it isn't a sinatra or a grape issue per se... this is a configuration issue. If you set sinatra downstream to grape, Sinatra will pick up all 404s and answer them. I think it is sufficient enough to leave this issue documented and the configuration instructions to solve it on this page. |
Do we need a doc update for https://github.com/ruby-grape/grape#alongside-sinatra-or-other-frameworks? |
@dblock yes - with the exact example in the doc, 404 would not be properly raised and caught by sinatra. You need change run Here's example code:
With API first:
With Web first:
|
Appreciate if you could PR this @jonmchan |
When I do:
error!({message: 'no data'}, 404)
or even simple oneerror!({}, 404)
I always get
text/html
for content-type with the response:"<!DOCTYPE html>\n<html>\n<head>\n <style type=\"text/css\">\n body { text-align:center;font-family:helvetica,arial;font-size:22px;\n color:#888;margin:20px}\n #c {margin:0 auto;width:500px;text-align:left}\n </style>\n</head>\n<body>\n <h2>Sinatra doesn’t know this ditty.</h2>\n <img src='http://localhost:7777/__sinatra__/404.png'>\n <div id=\"c\">\n Try this:\n <pre>get '/api/customers/111/info' do\n \"Hello World\"\nend\n</pre>\n </div>\n</body>\n</html>\n"
However, if I change to any other status code like
error!({}, 500)
, it returns the expected JSON response.The text was updated successfully, but these errors were encountered: