Always define explicit error sets for functions that can fail, rather than using inferred error sets. This makes error handling more maintainable and helps catch missing error cases during development. The error set should be defined at the function level, even for simple APIs.
Always define explicit error sets for functions that can fail, rather than using inferred error sets. This makes error handling more maintainable and helps catch missing error cases during development. The error set should be defined at the function level, even for simple APIs.
Example:
// Instead of:
pub fn init(alloc: Allocator, opts: rendererpkg.Options) !OpenGL {
// ...
}
// Do this:
const InitError = error{
OutOfMemory,
InvalidVersion,
DeviceNotFound,
};
pub fn init(alloc: Allocator, opts: rendererpkg.Options) InitError!OpenGL {
// ...
}
This approach:
Even for wrapper functions or simple APIs, defining the error set helps maintain consistency and makes future modifications safer by forcing explicit consideration of error cases.
Enter the URL of a public GitHub repository