-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
render_errors doesn't work with 2 options #1879
Comments
Can you provide more information on what isn't working for you with |
It's easy.
Stacktrace: |
It appears you are not sending the proper accept headers. When you specify both |
@chrismccord Fair enough. Explicit header will force phoenix to work render json. In this case Im lost with ideas here: Looks like I will need to add more logic around ErrorView/gut conn to determine content-type. |
Anyway it's not an error in phoenix. |
I ended up with decision that error while trying to render json is a bug. Phoenix can guess that I need to render json as an output w/o any format only when While html is rendered all the time not matter are there format=json or any headers. |
Can you please post your error view and how are you are sending requests to render such view? I see no reason why Phoenix would try to encode a map as HTML. |
Also, you can have the same behaviour as GitHub if you put JSON first in the formats list. If you have more than one format, then you must do content negotiation. |
I was able to resolve that issue. Thnx all! |
As phoenix not able to reject connection via api pipeline went to unknown path people are doing things like that http://stackoverflow.com/questions/38185875/how-to-enforce-json-encoding-for-phoenix-request |
Im reopening this again. No matter that @josevalim said that we should never enforce any content type at this point Phoenix with render_errors json will render json errors with Client is trying to open not existing page via pipeline :api in browser, Expected behaviour:
Got:
As I can see that is possible since pipelines from router.ex never kick in when routing errors been faced. Same problem will appear on using custom api like |
@noma4i I have just started a new application and I did the following to get always JSON responses:
With those changes, I get json no matter what. |
It was never problem here while you set whole app for json only. Issue is about the way 404/500 errors are treated in html/json pipelines.
Ok. I see the culprit. You talk about plug while https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/router.ex#L185
Is it expected behaviour to skip content negotiation on route errors? |
Yes, if no route matches we don't know which pipeline to invoke. Then you On Sunday, August 28, 2016, Alexander Tsirel [email protected]
José Valimwww.plataformatec.com.br |
@josevalim Iv extended tests for html/json ErrorViews + showing consequences of skipped content negotiation #1890 |
For now Phoenix has 2 routing pathways. 1 is matching routing 404 errors and the second - content of router.ex. In my case Iv created workaround to make content-type & format negotiation before router matching kicks in. That gives peace in mind that malformed or missing headers/format will never end up with |
@josevalim the I got an error so I am not sure about it. |
@yordis |
@noma4i aah, that is at the controller level, I thought this came from the thanks a lot for the explanation. |
Environment
Expected behavior
render_errors: [accepts: ~w(html json)]
Errors both works for html or json specified ways.
Actual behavior
html - ok
json -
Phoenix.Template.HTML.encode_to_iodata!(%{errors: %{message: "Not Found"}})
BUT! If you define only HTML or JSON at a time in
accepts
it will work correct.The text was updated successfully, but these errors were encountered: