How best to handle when DBConnection.start_link is having trouble connecting?

I have some code that uses start_link/2 to establish a connection and then immediately runs a query:

    {:ok, pid} = Postgrex.start_link(conn_opts)

    res = Postgrex.query(pid, statement, params)
    # ... close connection

Let’s say start_link is having trouble connecting because a bad password was given. Postgrex.query will fail due to a timeout - presumably because it’s waiting for start_link to boot.

What’s the best way to handle this programmatically? So that my program fails with the connection error as opposed to the timeout error?

I’m new to Elixir, so I assume the answer is related to GenServer specifics - which I’m still learning! :slight_smile: