Skip to content

Think hard about error handling / error types #68

Description

@remexre

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    !: help wantedExtra attention is neededP: majorMajor priority. "Normal" priority.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions