Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps reducing fmt usage. #365

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions exc/exc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package exc
import (
"errors"
"fmt"
"strconv"
)

// Exception is an error that designates a Cap'n Proto exception.
Expand All @@ -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 "<prefix>: <msg>".
// The type can be recovered using the TypeOf() function.
func New(typ Type, prefix, msg string) *Exception {
Expand All @@ -24,27 +45,30 @@ 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 "<prefix>: <msg>: <e>".
// If e.Prefix == prefix, the prefix will not be duplicated.
// 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 "<prefix>: <msg>: <err>".
Expand All @@ -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),
}
}

Expand Down