#Idiomatic way to format error messages

1 messages · Page 1 of 1 (latest)

hazy elk
#

I have an error set and I want to format it when I want to print the error set, but I cant put a format function in the error set. Whats the idiomatic way to do it

wide field
#

or use the default formatter if you want the full name (with namespace information)

hazy elk
#

like not just the name of the error

#

for example WindowError.CreationFail => "Failed to create window"

wide field
#

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

hazy elk
wide field
wide field
#

And logs can be overwritten and scoped easily

hazy elk
wide field
hazy elk
wide field
#

Its fairly standard

#

Only time you wouldn't is if you want them to be handled further down the line

vocal grove
#

your library could provide a error formatter, that takes in writer, error an optional context