From f2618b549082a1b3af1aeae577c43c8744d67fc7 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Fri, 25 Oct 2024 23:04:10 +0200 Subject: [PATCH] disko2: Move to error codes and dedicated error rendering This splits the concern of how to display errors off from the business logic. This should make it easier to test functionality and allow us to concentrate on how to actually display and communicate errors to the user. At some point this might also make i18n feasible, because you only have to swap out a single file. This hasn't quite fixed the debug/trace story yet. It should be possible to simply insert a "debug" command into any pipeline and see the context and state at that point. --- disko2 | 24 +++---- lib/errors.nu | 169 ++++++++++++++++++++++++++++++++++++++++----- lib/eval-config.nu | 16 ++--- lib/mod.nu | 2 +- 4 files changed, 169 insertions(+), 42 deletions(-) diff --git a/disko2 b/disko2 index 883c25d..7d54067 100755 --- a/disko2 +++ b/disko2 @@ -1,29 +1,25 @@ #!/usr/bin/env nu -use lib [eval-disko-file eval-flake p_err p_help exit-on-error] +use lib [eval-disko-file eval-flake exit-on-error print-info print-help] + +def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount"] } -def modes [] { ["destroy", "format", "mount", "format,mount", "destroy,format,mount"]} def main [ mode: string@modes, # Mode to use. Allowed values are 'destroy', 'format', 'mount', 'format,mount', 'destroy,format,mount' disko_file?: path, # File to read the disko configuration from. Can be a .nix file or a .json file --flake (-f): string # Flake URI to search for the disko configuration ]: nothing -> nothing { - + if not ($mode in (modes)) { - p_err $"Invalid mode: (ansi red)($mode)(ansi reset)" - let valid_modes = modes - | each { |mode| $"(ansi green)($mode)(ansi reset)" } - | str join ', ' - p_help $"Valid modes are: ($valid_modes)" - exit 1 + { + success: false, + messages: [ { code: ERR_INVALID_MODE, details: { mode: $mode, valid_modes: (modes) } } ] + } | exit-on-error "validate mode" } if not ($flake != null xor $disko_file != null) { - p_err "Missing arguments!" - p_help ($"Provide either (ansi magenta_italic)disko_file(ansi reset) as the second argument or " + - $"(ansi green)--flake(ansi reset)/(ansi green)-f(ansi reset) (ansi magenta_italic)flakeref(ansi reset)") - exit 1 + { success: false, messages: [ { code: ERR_MISSING_ARGUMENTS } ] } | exit-on-error "validate arguments" } let config = if $disko_file != null { @@ -32,5 +28,5 @@ def main [ $flake | eval-flake | exit-on-error "evaluate flake" } - $config | to json | print + $config | to json | print-info } diff --git a/lib/errors.nu b/lib/errors.nu index e859ab3..454afb7 100644 --- a/lib/errors.nu +++ b/lib/errors.nu @@ -1,26 +1,163 @@ use std log -export def exit-on-error [context: string] { - if $in.success { - log debug $"Success: ($context)" - log debug $"Return value: ($in.value)" - return $in.value +# Color definitions. NOTE: Sort them alphabetically when adding new ones! +def command [] { ansi cyan_italic } # Commands that were run or can be run +def file [] { ansi blue } # File paths +def flag [] { ansi green } # Command line flags (like --version or -f) +def invalid [] { ansi red } # Invalid values +def placeholder [] { ansi magenta_italic } # Values that need to be replaced +def value [] { ansi green } # Values that are allowed + +def reset [] { ansi reset } # Shortcut to reset the color + +def "lookup ERR_INVALID_MODE" [mode: string, valid_modes: list] { + let valid_modes = modes + | each { |mode| $"(value)($mode)(reset)" } + | str join ', ' + + [ + { + type: error + msg: $"Invalid mode: (invalid)($mode)(reset)" + } + { + type: help + msg: $"Valid modes are:\n($valid_modes)" + } + ] +} + +def "lookup ERR_MISSING_ARGUMENTS" [] { + [ + { + type: error + msg: $"Missing arguments!" + } + { + type: help + msg: ($"Provide either (placeholder)disko_file(reset) as the second argument or " + + $"(flag)--flake(reset)/(flag)-f(reset) (placeholder)flakeref(reset)") + } + ] +} + +def "lookup ERR_EVAL_CONFIG_FAILED" [ args, stderr ] { + [ + { + type: error + msg: ($"Failed to evaluate disko config with args (invalid)($args)(reset)!\n" + + $"Output from `(command)nix eval(reset)`: ($stderr)") + + } + ] +} + +def "lookup ERR_FILE_NOT_FOUND" [ path ] { + [ + { + type: error + msg: ($"File (file)($path)(reset) does not exist.") + } + ] +} + +def "lookup ERR_FLAKE_URI_NO_ATTRIBUTE" [ flakeUri ] { + [ + { + type: error + msg: ($"Flake URI (invalid)($flakeUri)(reset) does not contain an attribute.") + } + { + type: help + msg: $"Append an attribute like (value)#(placeholder)foo(reset) to the flake URI." + } + ] +} + +def lookup-message []: record> -> list> { + let code = $in.code + let details = $in.details? # Not all errors have details + match $code { + ERR_INVALID_MODE => (lookup ERR_INVALID_MODE $details.mode $details.valid_modes) + ERR_MISSING_ARGUMENTS => (lookup ERR_MISSING_ARGUMENTS) + ERR_EVAL_CONFIG_FAILED => (lookup ERR_EVAL_CONFIG_FAILED $details.args $details.stderr) + ERR_FILE_NOT_FOUND => (lookup ERR_FILE_NOT_FOUND $details.path) + ERR_FLAKE_URI_NO_ATTRIBUTE => (lookup ERR_FLAKE_URI_NO_ATTRIBUTE $details.flakeUri) + _ => [ + { + type: error + msg: $"Unknown error code: (invalid)($code)(reset)." + } + { + type: info + msg: $"The following details were provided:\n($details)" + } + { + type: help + msg: $"Please report this issue, this should never happen!" + } + ] + } +} + +def render-message []: record -> nothing { + let type = $in.type + let msg_lines = $in.msg | lines + + let bg_color = match $type { + error => (ansi bg_red) + help => (ansi bg_light_magenta) + info => (ansi bg_green) + } + let color = match $type { + error => (ansi red) + help => (ansi light_magenta) + info => (ansi green) + } + let title = match $type { + error => "ERROR" + help => " HELP" + info => " INFO" } - p_err $"Failed to ($context)!" - for msg in $in.messages { - print $"(ansi red)│(ansi reset) ($msg)" + if ($msg_lines | length) == 1 { + print $" ($bg_color) ($title): (reset) ($msg_lines.0)" + return + } + + print $"($color)╭─($bg_color) ($title): (reset) ($msg_lines.0)" + + for line in ($msg_lines | skip 1) { + print $"($color)│ (reset)($line)" + } + + print $"($color)╰─────────(reset)" +} + +def print-message []: record -> nothing { + $in | lookup-message | each { render-message } +} + +export def exit-on-error [context: string]: record -> any { + let result = $in + if $result.success { + log debug $"Success: ($context)" + log debug $"Return value: ($result.value)" + return $result.value + } else { + log debug $"Failure: ($context)" + } + + for message in $in.messages { + $message | print-message } - print $"(ansi red)╰────────(ansi reset)" exit 1 } -export def p_err [msg: string] { - print $"(ansi bg_red) ERROR: (ansi reset) ($msg)" +# Info and help messages may be printed directly, but errors not! That's why we don't have p-err here. +export def print-info []: string -> nothing { + { type: info, msg: $in } | render-message } -export def p_info [msg: string] { - print $"(ansi bg_green) INFO: (ansi reset) ($msg)" -} -export def p_help [msg: string] { - print $"(ansi bg_light_magenta) HELP: (ansi reset) ($msg)" +export def print-help []: string -> nothing { + { type: help, msg: $in } | render-message } \ No newline at end of file diff --git a/lib/eval-config.nu b/lib/eval-config.nu index ebb7d47..8bad9ce 100644 --- a/lib/eval-config.nu +++ b/lib/eval-config.nu @@ -1,5 +1,7 @@ use libexec-dir.nu +use std log + alias nix = ^nix --extra-experimental-features nix-command --extra-experimental-features flakes alias nix-eval-expr = nix eval --impure --json --expr @@ -11,10 +13,7 @@ def eval-config [args: record]: nothing -> record { | if $in.exit_code != 0 { { success: false - messages: [ - $"Failed to evaluate disko config with args ($args)!" - $"Error from nix eval: ($in.stderr)" - ] + messages: [ { code: ERR_EVAL_CONFIG_FAILED, details: { args: $args, stderr: $in.stderr } } ] } } else { $in.stdout @@ -32,9 +31,7 @@ export def eval-disko-file []: path -> record { if not ($config | path exists) { return { success: false - messages: [ - $"File (ansi blue)($config)(ansi reset) does not exist." - ] + messages: [ { code: ERR_FILE_NOT_FOUND, details: { path: $config } } ] } } @@ -56,10 +53,7 @@ export def eval-flake []: string -> record { } else { return { success: false - messages: [ - $"Flake-uri ($flakeUri) does not contain an attribute." - "Please append an attribute like \"#foo\" to the flake-uri." - ] + messages: [ { code: ERR_FLAKE_URI_NO_ATTRIBUTE, details: { flakeUri: $flakeUri } } ] } } diff --git a/lib/mod.nu b/lib/mod.nu index 6135208..f13d82d 100644 --- a/lib/mod.nu +++ b/lib/mod.nu @@ -1,2 +1,2 @@ export use eval-config.nu [eval-disko-file eval-flake] -export use errors.nu [exit-on-error p_err p_info p_help] +export use errors.nu [exit-on-error print-info print-help]