sahilpaudel
404 is rendered as 500 in staging and production
I have route say /status
If I hit the api /status it gives 200 which is fine.
Now when I hit /status1 it gives 500 in production but 404 in dev.
I tried spotting the difference and found that if I change my prod.exs file as
config :my_app, MyApp.Web.Endpoint,
load_from_system_env: true,
url: [port: 4000],
server: true,
debug_errors: true // introduce this
It is working as expected but I read somewhere we shouldn’t add debug_errors in prod.
Can you please suggest on this.
Marked As Solved
stefanchrobot
This is exactly the right approach. Not sure why your codebase diverged from that, but here are the relevant fragments from a freshly generated Phoenix API project (--no-html --no-webpack):
# config/dev.exs
config :test, TestWeb.Endpoint,
render_errors: [view: TestWeb.ErrorView, accepts: ~w(json), layout: false],
# ...
# lib/test_web/views/error_view.ex
defmodule TestWeb.ErrorView do
use TestWeb, :view
# If you want to customize a particular status code
# for a certain format, you may uncomment below.
# def render("500.json", _assigns) do
# %{errors: %{detail: "Internal Server Error"}}
# end
# By default, Phoenix returns the status message from
# the template name. For example, "404.json" becomes
# "Not Found".
def template_not_found(template, _assigns) do
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end
end
Also Liked
stefanchrobot
Can you please share the logs? The NoRouteError exception should be converted to 404, so it may be something else that’s exploding on production.
stefanchrobot
The one generated by default is perfectly fine. Are there any concerns that you’d like to address?
Phoenix is not really an opinionated framework, so you can structure your web folder either by module kind (controller, view, etc) or by business use case (registration, user profile, etc.).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









