Closed maranqz closed 1 year ago
Try this:
ErrDBNotFound = fmt.Errorf("object not found: %w", ErrDB)
Alternatively, you can indeed add a custom error object:
type notFound struct {
error
}
// This is important.
func (e *notFound) FormatError(p errors.Printer) error {
p.Printf("%s: not found", e.error.Error())
return nil
}
func (e *notFound) Unwrap() error { return e.error }
func (e *notFound) Error() string { return fmt.Sprint(errors.Formattable(e)) }
var ErrDBNotFound = ¬Found{ErrDB}
Thank you, but I want db: not found
to show that not found
is error of db
like math/big: some error
or sql: some error
in built in libraries.
Why extractPrefix was added?
Thank you, but I want db: not found
Then you can build a custom type as suggested above.
Why extractPrefix was added?
To enable:
%+v
is passed
Hello. Thank you for the Library and error handling RFC.
I have error ErrDB and the child of that ErrDBNotFound. Then I enrich Err with stack and want to get message "db: not found", but I get "db". I found that
extractPrefix
cut the part of message, could you give advice how to fix it and whyextractPrefix
was added?https://go.dev/play/p/gqT2QOTMcRn
Solution, which I see, but I hope there is more elegant solution and explanation.
fmt.Errorf("%w: %w", ErrDB, ErrDBNotFound)
WithMessage
or custom Err with changingfunc Error() string