ProcessError

The ProcessError type provides structured error handling for process and command execution operations. Used by Process module functions like Process.run, Process.run-capture, and Process.spawn.

match Process.run-capture "ls" ["-la"]
| Ok output -> println output.stdout
| Err ExecutionFailed{command, message} -> println "Failed: ${message}"
| Err Timeout{command} -> println "Timed out: ${command}"
| Err ExitCode{command, code} -> println "Exit code ${code}"
See Also: Contracts

For preventing errors through preconditions and postconditions, see Contracts. Use @pre to validate inputs before operations that might fail.

Constructors

ProcessError
type ProcessError = ExecutionFailed{...} | Timeout{...} | ...
ConstructorFieldsDescription
ExecutionFailed command: String, message: String The command failed to execute (e.g., command not found)
Timeout command: String The process exceeded its time limit
ExitCode command: String, code: Int The process exited with a non-zero exit code
SignalTerminated command: String, signal: Int The process was terminated by a signal
ProcessError message: String Generic process error

Trait Implementations

Show

Provides human-readable error descriptions:

err = ExitCode{command: "make", code: 2}
println (show err)
# => ExitCode: make (2)

err = Timeout{command: "slow-script"}
println (show err)
# => Timeout: slow-script

Error

Implements the standard Error trait with message, kind, and code:

ConstructorError KindError Code
ExecutionFailed:execution-failedNone
Timeout:timeoutNone
ExitCode:exit-codeSome code
SignalTerminated:signal-terminatedSome signal
ProcessError:process-errorNone

Note: ExitCode and SignalTerminated errors provide their numeric codes via Error.code.

Examples

Running a Build Command

run-build = fn() =>
  match Process.run-capture "make" ["build"]
  | Ok output ->
    println "Build succeeded!"
    println output.stdout
    Ok no-op
  | Err ExitCode{code, ..} ->
    println "Build failed with exit code ${code}"
    Err code
  | Err ExecutionFailed{message, ..} ->
    println "Could not run make: ${message}"
    Err -1
  | Err e ->
    println "Error: ${Error.message e}"
    Err -1

Handling Timeouts

run-with-timeout = fn(cmd, args, timeout-ms) =>
  match Process.run-capture-timeout cmd args timeout-ms
  | Ok output -> Ok output.stdout
  | Err Timeout{command} ->
    println "Command '${command}' timed out after ${timeout-ms}ms"
    Err "timeout"
  | Err e -> Err (Error.message e)

Checking Exit Codes

run-and-check = fn(cmd, args) =>
  match Process.run-capture cmd args
  | Ok output -> true
  | Err ExitCode{code, ..} ->
    match code
    | 0 -> true   # Success
    | 1 -> false  # General error
    | 2 -> false  # Misuse of command
    | _ -> false  # Other errors
  | Err _ -> false