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

HTML dumps in console some times when testing API endpoints. #25226

Closed
hmistry opened this issue May 31, 2016 · 10 comments
Closed

HTML dumps in console some times when testing API endpoints. #25226

hmistry opened this issue May 31, 2016 · 10 comments
Assignees

Comments

@hmistry
Copy link
Contributor

hmistry commented May 31, 2016

I'm filing this issue to bring to your attention in case no one else has... it might have been addressed but I couldn't find any in a quick search.

Steps to reproduce

I will update with more specific steps when I encounter it again. Sorry.

I'm using Rails 5rc1 to develop a mixed web and API rails app. Some times when things go wrong, I get html responses (some times there's even references to web-console) during testing the API only endpoints in development.

Have an API endpoint that returns json normally. Send a invalid request using curl in the console, and you'll receive a rails error html dump to your console.

Updated with steps:

  • Rails 5rc1 app with mixed HTML and API responses.
  • All HTML routes go to controllers that inherit from ApplicationController which inherits from ActionController::Base.
  • All API routes go to controllers that inherit from ApiApplicationController which inherits from ActionController::API.
  • HTML responses are HTML web pages, and API responses are JSON.

Use curl in a console to visit an invalid route and you'll see the HTML error page response in your console.

Expected behavior

I expect a plain text message with the error that is better formatted for the console.

Actual behavior

HTML web page dump response to console.

System configuration

Rails version: 5.0.0.rc1

Ruby version: 2.3.0

@maclover7
Copy link
Contributor

cc @gsamokovarov

@prathamesh-sonpatki
Copy link
Member

@hmistry c33bda8 should fix it for API-only apps.

For mixed apps, do give a try with config config.debug_exception_response_format = :api.

Let me know if it doesn't work and I will reopen.

@hmistry
Copy link
Contributor Author

hmistry commented Jun 1, 2016

@prathamesh-sonpatki I tried your config setting. It sort of worked...

  • For the API endpoint, I got a JSON response of the error and trace. This makes sense and its a good response for API, better than what I had in mind.
  • For the HTML endpoint, I got a JSON response too. This doesn't makes sense to me, from a holistic application perspective. If I go to a HTML endpoint, I expect to be receiving HTML response unless I request otherwise. I also lost the ability to debug using the web console for the HTML endpoints.

I think the correct behavior is for Rails to give an error message with exception/traces in JSON response for API endpoints and a normal HTML response for HTML endpoints. Without knowing the internals of Rails, I think if some thing inherits from ActionController::API then use the JSON response and if it inherits from ActionController::Base then use the HTML response... just an idea but you guys know Rails internals way way better than me.

Looking at the API trace response, I see web-console middleware exists in the API stack. It would be nice if its bypassed or even better eliminated since you'll never use web-console in an API endpoint, but have it present for the HTML endpoint. This is an ideal nicety in reducing unnecessary overhead, but definitely not a showstopper. 😃

@prathamesh-sonpatki
Copy link
Member

@hmistry For Rails 4.x the default behavior was to always show HTML responses for even JSON requests too.

With API apps, this got changed to JSON when config.debug_exception_response_format = :api and HTML for config.debug_exception_response_format = :default. But it introduced an issue for eg. when you have HTML endpoint alongside API endpoint and you set config.debug_exception_response_format = :api then you get JSON responses for your HTML endpoints too, as you found out. This issue was present in 5.0 RC1.

But it got fixed in c33bda8. So on master, if you use config.debug_exception_response_format = :api then you should get HTML response for HTML request and JSON response for JSON response. The config must be set to :api, otherwise you will get HTML responses for both endpoints. I am working on making this more useful for having the behavior of preserving request format for all requests not just depending on config.debug_exception_response_format.

Regarding web console, for API only apps web-console gem is not present in the Gemfile. But I think you have normal app, so you are getting it in the stack traces. Not sure how to include it based on request type. Also it might be surprising behavior for some cases.

@gsamokovarov
Copy link
Contributor

We shouldn't be even trying to show the console in the JSON responses, if we detect such, so this shouldn't be a problem IMO. You'll get the console only for HTML error responses, where we can properly show it and it may be useful for you to diagnose the problem.

If we do show it in a JSON response, then please report it as a bug to rails/web-console and we'll look into it.

@hmistry
Copy link
Contributor Author

hmistry commented Jun 4, 2016

@prathamesh-sonpatki I'm unable to verify your patch because I'm having trouble updating rails to that commit or master as bundler is failing to resolve compatible versions. I even tried to create a new app using Rails master without any luck.

Let me know if you have any ideas in how I can get around the gem compatibility issues, let me know, otherwise I'll have to wait to 5.0.0.rc2 release to verify it.

@hmistry
Copy link
Contributor Author

hmistry commented Jun 9, 2016

@prathamesh-sonpatki Let's re-open this issue since you verified yesterday that the fix in c33bda8 does not give the appropriate response in a mixed app. This way the bug can be tracked.

@prathamesh-sonpatki
Copy link
Member

Assigning to myself. Just a clarification, this is not a blocker for 5.0 release.

@hmistry
Copy link
Contributor Author

hmistry commented Feb 28, 2017

Any update for a fix on this issue?

@rails-bot rails-bot bot added the stale label May 30, 2017
@rails-bot
Copy link

rails-bot bot commented May 30, 2017

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot closed this as completed Jun 6, 2017
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

4 participants