#Idiomatic way to format error messages
1 messages · Page 1 of 1 (latest)
you can use @errorName (mirroring @tagName for enums)
or use the default formatter if you want the full name (with namespace information)
yeah but i want to use cusotm error messages yk
like not just the name of the error
for example WindowError.CreationFail => "Failed to create window"
ah
cant add a format method to an error set so i’d just do a fn formatWinError(writer: anytype, e: WIndowError) !void { … }
I personally wouldnt do it like that though since most errors can’t just be “decoded” without a ton of context that only the caller knows
like maybe itll work for you but I doubt it, especially with windows apis
the same error code can mean a million different things depending on the context
basically I want to log errors with a nice message, before I was printing at the error site but that seemed like the improper way to do it in zig
if you just want to log it, I’d use the builtin std.log system
you can even add your own scope and use a custom logging function
should I log at the error site
Yeah thats where you have the most information
And logs can be overwritten and scoped easily
wdym overwritten
*overriden
you can use std.Options to change the behavior easily
alr, but it still feels weird to log errors at the error site
Its fairly standard
Only time you wouldn't is if you want them to be handled further down the line
your library could provide a error formatter, that takes in writer, error an optional context