Improve error handling

This commit is contained in:
Riyyi
2026-08-09 21:06:59 +02:00
parent af5db96962
commit 9a4e61626c
9 changed files with 110 additions and 72 deletions
+22
View File
@@ -17,6 +17,12 @@ File_Error :: enum u8 {
Okay = None,
}
file_error_strings := #sparse[File_Error]string { // enumerated array
.None = "",
.Not_A_Directory = "path is not a directory",
.Unimplemented = "feature is unimplemented",
}
// -----------------------------------------
get_working_dir :: proc(allocator := context.allocator) -> (string, Error) {
@@ -28,3 +34,19 @@ get_working_dir :: proc(allocator := context.allocator) -> (string, Error) {
return wd, nil
}
@(require_results)
format_error :: proc(ferr: Error) -> string {
if ferr == nil do return ""
switch e in ferr {
case nil:
return ""
case os.Error:
return fmt.tprintf("os: {}", e)
case File_Error:
return fmt.tprintf("file: {}", file_error_strings[e])
}
return "unknown error"
}