Skip to content

Commit

Permalink
refactor the error makes in error/error.nu (#60)
Browse files Browse the repository at this point in the history
this PR is a simple refactoring to have a single standardized way to
throw errors in `gm` 😋
  • Loading branch information
amtoine authored Nov 4, 2023
1 parent 4935705 commit a360ea9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/nu-git-manager/error/error.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# throw a nice error with a standard format
export def throw-error [
error: record<msg: string, label: record<text: string, span: record<start: int, end: int>>>
] {
error make {
msg: $"(ansi red_bold)($error.msg)(ansi reset)"
label: {
text: $error.label.text
start: $error.label.span.start
end: $error.label.span.end
}
}
}
35 changes: 18 additions & 17 deletions src/nu-git-manager/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use fs/cache.nu [
save-cache, clean-cache-dir
]
use git/url.nu [parse-git-url, get-fetch-push-urls]
use error/error.nu [throw-error]

def "nu-complete git-protocols" []: nothing -> table<value: string, description: string> {
[
Expand Down Expand Up @@ -77,13 +78,14 @@ export def "gm clone" [
| path join

if ($local_path | path exists) {
let span = metadata $url | get span
error make {
msg: $"(ansi red_bold)repository_already_in_store(ansi reset)"
throw-error {
msg: "repository_already_in_store"
label: {
text: $"this repository has already been cloned by (ansi {fg: "default_dimmed", attr: "it"})gm(ansi reset)"
start: $span.start
end: $span.end
text: (
"this repository has already been cloned by "
+ $"(ansi {fg: "default_dimmed", attr: "it"})gm(ansi reset)"
)
span: (metadata $url | get span)
}
}
}
Expand All @@ -93,13 +95,11 @@ export def "gm clone" [
mut args = [$urls.fetch $local_path --origin $remote]
if $depth != null {
if ($depth < 1) {
let span = metadata $depth | get span
error make {
msg: $"(ansi red_bold)invalid_clone_depth(ansi reset)"
throw-error {
msg: "invalid_clone_depth"
label: {
text: $"clone depth should be strictly positive, found ($depth)"
start: $span.start
end: $span.end
span: (metadata $depth | get span)
}
}
}
Expand Down Expand Up @@ -258,13 +258,14 @@ export def "gm remove" [

let repo_to_remove = match ($choices | length) {
0 => {
let span = metadata $pattern | get span
error make {
msg: $"(ansi red_bold)no_matching_repository(ansi reset)"
throw-error {
msg: "no_matching_repository"
label: {
text: $"no repository matching this in (ansi {fg: "default_dimmed", attr: "it"})($root)(ansi reset)"
start: $span.start
end: $span.end
text: (
"no repository matching this in "
+ $"(ansi {fg: "default_dimmed", attr: "it"})($root)(ansi reset)"
)
span: (metadata $pattern | get span)
}
}
},
Expand Down

0 comments on commit a360ea9

Please sign in to comment.