It would be really nice if we could have more detailed errors than a single errno. However, it's also important to consider the system's behavior in low- or out-of-memory situations (i.e., it probably shouldn't allocate, or at least allocation should be optional).
In a previous doodle of a userspace API, I'd made errors fixed-size buffers with statically-allocated descriptions. The caller of a function passes down an error buffer and its usable size, and the callee populates it on error. If the buffer fills up, a flag gets set for "this is truncated" and previous "error frames" beyond the first get overwritten.
struct errors {
u32 len, cap; // stored as n*2 by default; on truncation, len is n*2+1 instead
const struct error **ptr;
};
struct error {
usize len;
char description[len];
};
With some macro + inline asm + linker cleverness, it should be possible to have a push_cause macro that compiles in the filename and line number, plus gets the struct error mapped to the right area of memory:
bool foo(struct errors* errs) {
if (!bar(errs)) {
// actual `struct error` says "foo.c:4: failed to pass the bar"
push_cause(errs, "failed to pass the bar");
return false;
}
// ...
return true;
}
That could itself be wrapped:
bool foo(struct errors* errs) {
try(bar(errs), "failed to pass the bar");
// ...
return true;
}
This feels like the right kind of thing, but I'm leery of implementing it without exploring the design space more.
So, this idea is soliciting feedback on this, and asking for different ideas people have.
It would be really nice if we could have more detailed errors than a single
errno. However, it's also important to consider the system's behavior in low- or out-of-memory situations (i.e., it probably shouldn't allocate, or at least allocation should be optional).In a previous doodle of a userspace API, I'd made errors fixed-size buffers with statically-allocated descriptions. The caller of a function passes down an error buffer and its usable size, and the callee populates it on error. If the buffer fills up, a flag gets set for "this is truncated" and previous "error frames" beyond the first get overwritten.
With some macro + inline asm + linker cleverness, it should be possible to have a
push_causemacro that compiles in the filename and line number, plus gets thestruct errormapped to the right area of memory:That could itself be wrapped:
This feels like the right kind of thing, but I'm leery of implementing it without exploring the design space more.
So, this idea is soliciting feedback on this, and asking for different ideas people have.