How to intercept Ecto connection failures when starting Phoenix

hi Everyone,

I would like my Phoenix app to exit when password authentication failed.

[error] Postgrex.Protocol (#PID<0.540.0>) failed to connect: 
    ... ** (Postgrex.Error) FATAL 28P01 (invalid_password): 
    ... password authentication failed for user "postgres"

an exception can be raised in the Repo module when the database config is missing:

def init(_, opts) do
  url = System.get_env("DATABASE_URL") || raise "DATABASE_URL not set"
  # etc
end

when the database config is invalid, Phoenix / Ecto / Postgrex keeps on trying to connect indefinitely.
of course the app will never run properly, so I would like it to stop running immediately.

also in case of an invalid_catalog_name the app should exit:

[error] Postgrex.Protocol (#PID<0.321.0>) failed to connect: 
    ... ** (Postgrex.Error) FATAL 3D000 (invalid_catalog_name): 
    ... database "wrong_database" does not exist

it would be great to also be able to log the exceptions, so I can tell what went wrong.
also when running Phoenix as an OTP app.

tnx for any suggestions.

1 Like