krishna_vaguelyright

krishna_vaguelyright

How to handle errors centrally?

How to handle errors centrally?

In my local instance, it throws the error with proper error codes. On prod deployed to fly, it just throws a 500 status error.

In my tests I have to use assert_error_sent to make tests pass. When i try to assert conn.status it just throws an error in test module.

conn = post(conn, `route`, %{})
assert conn.status == 400

This is what my code looks like… works fine in dev locally but throws 500 error in prod.

defmodule KayaanPrintsWeb.Router do
  use KayaanPrintsWeb, :router
  use Plug.ErrorHandler
  require Logger

 
  @impl true
  def handle_errors(conn, %{kind: kind, reason: reason, stack: stack}) do
    error_string = log_error_string(kind, reason, stack, conn)

    case reason do
      %Phoenix.Router.NoRouteError{} ->
        Logger.error(error_string)

        conn
        |> put_status(:not_found)
        |> json(%{error: "Route not found"})
        |> halt()
  
     ....other error cases handling

Most Liked

LostKobrakai

LostKobrakai

If your test fails your code has a problem.

This likely works in dev, because phoenix enables Plug.Debugger in development. There can only ever be one error handler though, so your Plug.ErrorHandler might not even run.

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New

We're in Beta

About us Mission Statement