sahilpaudel

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

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

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

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.).

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement