Skip to content

A question about error handling in the executed command #90

Description

@oderwat

Am I correct that if I want to handle errors from the actual running command I needs to use run_and_handle() and handle errors myself?

This would for example need to including adding another "exit(1)" implementation.

I actually thought at first that returning Error(String) would print the string and exit(1). It also seems that I can't have anything else than Error(String) with that, where I would maybe like to use snag. So I would probably even need to add my own Result type as Ok(Result(Nil,EApp))?

I probably miss something, and it is supposed to work differently. I skimmed some other apps using Glint and found mostly "let assert ..." or panic style "error handling".

Here some code to illustrate what I mean:

pub fn main() -> Nil {
  let cmds =
    glint.new()
    |> glint.with_name(tomlvars.name)
    |> glint.add(at: [], do: run())
  use out <- glint.run_and_handle(cmds, argv.load().arguments)
  case out {
    Error(err) -> {
      io.print_error("Error: " <> err)
      exit(1)
    }
    _ -> Nil
  }
}

fn run() -> glint.Command(Result(Nil, String)) {
  use <- glint.command_help("import a csv to the welo marktordnung")
  use <- glint.unnamed_args(glint.EqArgs(0))
  use arg_csv <- glint.named_arg("csv")
  use arg_periode <- glint.named_arg("periode")
  use named, _unnamed, _flags <- glint.command()

  // if this fails it will end up in the `case out` above and has to be `Error(String)`
  use csv_data <- result.try(read_csv(arg_csv(named)))

  // same with this
  use periode <- result.try(welo.parse_dbdate(arg_periode(named)))

  let con = connect()
  echo csv_data
  echo periode
  echo con
  Ok(Nil)
}

@external(erlang, "erlang", "halt")
@external(javascript, "node:process", "exit")
fn exit(status: Int) -> Nil

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