From ad4ab3858b76db1525f61bc83fe8e02f318d20f4 Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Sun, 4 Dec 2022 18:29:58 -0500 Subject: [PATCH] First steps reducing fmt usage. Per #364, this is a first step on the road to eliminating usage of fmt from the library; it removes most direct uses of it from the exc package. The remaining bits are Failedf and friends. I'd like to just remove these, but that requires changing every downstream call site, and I wanted to keep this patch small for today; I can tackle that when I have a bit more time. I don't think it will be too hard; from a quick skim WrapError covers 99% of the use cases, and the rest won't be hard to replace with things from strconv. --- exc/exc.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/exc/exc.go b/exc/exc.go index ea2a5d6b..846bcf4e 100644 --- a/exc/exc.go +++ b/exc/exc.go @@ -4,6 +4,7 @@ package exc import ( "errors" "fmt" + "strconv" ) // Exception is an error that designates a Cap'n Proto exception. @@ -13,6 +14,26 @@ type Exception struct { Cause error } +type wrappedError struct { + prefix string + base error +} + +func (e wrappedError) Error() string { + return e.prefix + ": " + e.base.Error() +} + +func (e wrappedError) Unwrap() error { + return e.base +} + +func WrapError(prefix string, err error) error { + return wrappedError{ + prefix: prefix, + base: err, + } +} + // New creates a new error that formats as ": ". // The type can be recovered using the TypeOf() function. func New(typ Type, prefix, msg string) *Exception { @@ -24,16 +45,19 @@ func (e Exception) Error() string { return e.Cause.Error() } - return fmt.Sprintf("%s: %v", e.Prefix, e.Cause) + return WrapError(e.Prefix, e.Cause).Error() } func (e Exception) Unwrap() error { return e.Cause } func (e Exception) GoString() string { - return fmt.Sprintf("errors.Error{Type: %s, Prefix: %q, Cause: fmt.Errorf(%q)}", - e.Type.GoString(), - e.Prefix, - e.Cause) + return "errors.Error{Type: " + + e.Type.GoString() + + ", Prefix: " + + strconv.Quote(e.Prefix) + + ", Cause: " + + strconv.Quote(e.Cause.Error()) + + "}" } // Annotate is creates a new error that formats as ": : ". @@ -41,10 +65,10 @@ func (e Exception) GoString() string { // The returned Error.Type == e.Type. func (e Exception) Annotate(prefix, msg string) *Exception { if prefix != e.Prefix { - return &Exception{e.Type, prefix, fmt.Errorf("%s: %w", msg, e)} + return &Exception{e.Type, prefix, WrapError(msg, e)} } - return &Exception{e.Type, prefix, fmt.Errorf("%s: %w", msg, e.Cause)} + return &Exception{e.Type, prefix, WrapError(msg, e.Cause)} } // Annotate creates a new error that formats as ": : ". @@ -62,7 +86,7 @@ func Annotate(prefix, msg string, err error) *Exception { return &Exception{ Type: Failed, Prefix: prefix, - Cause: fmt.Errorf("%s: %w", msg, err), + Cause: WrapError(msg, err), } }