Skip to content

Improve error when port is unavailable #80

@daniellionel01

Description

@daniellionel01

Hi!

I went down a debugging rabbit hole when one application was taking up port 3000 so the one I was currently working on could not be run. However the error that it was giving me:

Error! Failed to eval: max@@main:run(max)

Runtime terminating during boot ({{bad_generator,{failed_to_start_child,0,{init_failed,<<"shutdown">>}}},[{'max@@main','-print_error/3-lc$^0/1-0-',1,[{file,"/app/max/build/dev/erlang/max/_gleam_artefacts/max@@main.erl"},{line,40}]},{'max@@main',print_error,3,[{file,"/app/max/build/dev/erlang/max/_gleam_artefacts/max@@main.erl"},{line,40}]},{'max@@main',run,1,[{file,"/app/max/build/dev/erlang/max/_gleam_artefacts/max@@main.erl"},{line,16}]},{erl_eval,do_apply,7,[{file,"erl_eval.erl"},{line,920}]},{init,start_it,1,[]},{init,start_em,1,[]},{init,do_boot,3,[]}]})

Did not give me a good indication of what was actually going on. I know from other frameworks / libraries that they offer a hint or specific error when the port is in use. Example from a Bun Web Server:

$ bun run index.ts
1 | const server = Bun.serve({
                       ^
error: Failed to start server. Is port 3001 in use?
 syscall: "listen",
   errno: 0,
    code: "EADDRINUSE"

      at /app/index.ts:1:20
      at loadAndEvaluateModule (2:1)

Here was my setup in the gleam app:

fn start() -> Nil {
  let _ = env.get_env()

  let assert Ok(_supervisor) =
    static_supervisor.new(static_supervisor.OneForOne)
    |> static_supervisor.add(start_server())
    |> static_supervisor.start

  process.sleep_forever()
}


fn start_server() {
  let env = env.get_env()

  wisp.configure_logger()

  let assert Ok(priv) = wisp.priv_directory("max")
  let static_directory = priv <> "/static"

  let secret_key_base = env.web_secret_key_base

  use connection <- db.with_connection()
  let context = web.Context(static_directory:, connection:)

  router.handle_request(_, context)
  |> wisp_mist.handler(secret_key_base)
  |> mist.new
  |> mist.bind("0.0.0.0")
  |> mist.port(env.web_port)
  |> mist.supervised
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions