From e5263915b9548362c6b50ea324f25f9a0ad889a0 Mon Sep 17 00:00:00 2001 From: Colin Date: Fri, 30 Aug 2024 12:42:28 +0000 Subject: [PATCH] refactor: namespace: leverage errors::ext::swallow where easily applicable --- pkgs/additional/bunpen/errors/ext/check.ha | 29 ++++++++++++++------ pkgs/additional/bunpen/restrict/namespace.ha | 14 ++++------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pkgs/additional/bunpen/errors/ext/check.ha b/pkgs/additional/bunpen/errors/ext/check.ha index 9a670d18c..4cdeab84f 100644 --- a/pkgs/additional/bunpen/errors/ext/check.ha +++ b/pkgs/additional/bunpen/errors/ext/check.ha @@ -3,9 +3,10 @@ use fmt; use fs; use log; use os::exec; +use path; use rt; -export type error = (fs::error | os::exec::error | rt::errno); +export type error = (fs::error | os::exec::error | path::error | rt::errno); // stringify an error. return value is statically allocated, no need to free. export fn maybe_strerror(what: (void | ...error)) (void | str) = { @@ -13,6 +14,7 @@ export fn maybe_strerror(what: (void | ...error)) (void | str) = { return match (what) { case let e: fs::error => yield fs::strerror(e); case let e: os::exec::error => yield os::exec::strerror(e); + case let e: path::error => yield path::strerror(e); case let e: rt::errno => // N.B.: this panics if the buffer is too small. yield fmt::bsprintf(errorbuf[..], "{}: {}", rt::errname(e), rt::strerror(e)); @@ -27,19 +29,30 @@ export fn strerror(what: error) str = { }; }; -export fn check(context: str, what: (void | ...error)) void = { +export fn check(context: str, what: (void | ...error), fmt_args: fmt::field...) void = { + let context_buf: [4096]u8 = [0...]; match (maybe_strerror(what)) { - case let s: str => log::fatalf("{}: {}", context, s); + // N.B.: use `fatal(...)` over `fatalf("{}: {}")` so that my treelogger + // can extract the context. + case let errstr: str => log::fatal( + fmt::bsprintf(context_buf[..], context, fmt_args...), + ":", + errstr, + ); case void => void; }; }; -export fn swallow(context: str, what: (void | ...error)) void = { +export fn swallow(context: str, what: (void | ...error), fmt_args: fmt::field...) void = { + let context_buf: [4096]u8 = [0...]; match (maybe_strerror(what)) { - case let s: str => - // N.B.: use `println(...)` over `printfln("{}: {}")` so that my treelogger - // can extract the context. - log::println(context, ":", s); + // N.B.: use `println(...)` over `printfln("{}: {}")` so that my treelogger + // can extract the context. + case let errstr: str => log::println( + fmt::bsprintf(context_buf[..], context, fmt_args...), + ":", + errstr, + ); case void => void; }; }; diff --git a/pkgs/additional/bunpen/restrict/namespace.ha b/pkgs/additional/bunpen/restrict/namespace.ha index c09c9e16a..554ed21c8 100644 --- a/pkgs/additional/bunpen/restrict/namespace.ha +++ b/pkgs/additional/bunpen/restrict/namespace.ha @@ -202,15 +202,11 @@ fn bind_leaf(old_fs: *fs::fs, new_fs: *fs::fs, user_path: *path::buffer) void = yield other; }; - match (bind_component(old_fs, new_fs, cur_strpath, path::iterrem(&it))) { - case let e: fs::error => - log::printfln("[namespace] unable to copy intermediate path {} of {}: {}", cur_strpath, path_str, fs::strerror(e)); - return; - case let e: path::error => - log::printfln("[namespace] unable to copy intermediate path {} of {}: {}", cur_strpath, path_str, path::strerror(e)); - return; - case void => void; - }; + errors::ext::swallow( + "[namespace] unable to copy intermediate path {} of {}", + bind_component(old_fs, new_fs, cur_strpath, path::iterrem(&it)), + cur_strpath, path_str + ); }; // and now, perform the actual bind mount: