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

safe details/messages: clarify cases with empty formats #3

Merged
merged 3 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion barriers/barriers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (e *barrierError) Format(s fmt.State, verb rune) { errbase.FormatError(e, s
func (e *barrierError) FormatError(p errbase.Printer) (next error) {
p.Print(e.msg)
if p.Detail() {
p.Printf("\noriginal cause behind barrier:\n%+v", e.maskedErr)
p.Printf("\noriginal cause behind barrier: %+v", e.maskedErr)
}
return nil
}
Expand Down
21 changes: 7 additions & 14 deletions barriers/barriers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,27 @@ func TestFormat(t *testing.T) {
}{
{"handled", barriers.Handled(goErr.New("woo")), woo, `
woo:
original cause behind barrier:
woo`},
original cause behind barrier: woo`},

{"handled + handled", barriers.Handled(barriers.Handled(goErr.New("woo"))), woo, `
woo:
original cause behind barrier:
woo:
original cause behind barrier:
woo`},
original cause behind barrier: woo:
original cause behind barrier: woo`},

{"handledmsg", barriers.HandledWithMessage(goErr.New("woo"), "waa"), "waa", `
waa:
original cause behind barrier:
woo`},
original cause behind barrier: woo`},

{"handledmsg + handledmsg", barriers.HandledWithMessage(
barriers.HandledWithMessage(
goErr.New("woo"), "waa"), "wuu"), `wuu`, `
wuu:
original cause behind barrier:
waa:
original cause behind barrier:
woo`},
original cause behind barrier: waa:
original cause behind barrier: woo`},

{"handled + wrapper", barriers.Handled(&werrFmt{goErr.New("woo"), "waa"}), waawoo, `
waa: woo:
original cause behind barrier:
waa:
original cause behind barrier: waa:
-- verbose wrapper:
waa
- woo`},
Expand Down
19 changes: 19 additions & 0 deletions errutil/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package errutil
import (
"github.com/cockroachdb/errors/assert"
"github.com/cockroachdb/errors/barriers"
"github.com/cockroachdb/errors/withstack"
)

// AssertionFailedf creates an internal error.
Expand All @@ -38,6 +39,24 @@ func AssertionFailedWithDepthf(depth int, format string, args ...interface{}) er
return err
}

// HandleAsAssertionFailure hides an error and turns it into
// an assertion failure. Both details from the original error and the
// context of the caller are preserved. The original error is not
// visible as cause any more. The original error message is preserved.
// See the doc of `AssertionFailedf()` for more details.
func HandleAsAssertionFailure(origErr error) error {
return HandleAsAssertionFailureDepth(1, origErr)
}

// HandleAsAssertionFailureDepth is like HandleAsAssertionFailure but
// the depth at which the call stack is captured can be specified.
func HandleAsAssertionFailureDepth(depth int, origErr error) error {
err := barriers.Handled(origErr)
err = withstack.WithStackDepth(err, 1+depth)
err = assert.WithAssertionFailure(err)
return err
}

// NewAssertionErrorWithWrappedErrf wraps an error and turns it into
// an assertion error. Both details from the original error and the
// context of the caller are preserved. The original error is not
Expand Down
120 changes: 118 additions & 2 deletions errutil/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ error with attached stack trace:
- waa: hello`,
},

{"newf-empty",
errutil.Newf(emptyString),
``, `
error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- `,
},

{"newf-empty-arg",
errutil.Newf(emptyString, 123),
`%!(EXTRA int=123)`, `
error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- error with embedded safe details:
-- arg 1: <int>
- %!(EXTRA int=123)`,
},

{"wrapf",
errutil.Wrapf(goErr.New("woo"), "waa: %s", "hello"),
`waa: hello: woo`, `
Expand All @@ -65,6 +93,54 @@ error with attached stack trace:
- woo`,
},

{"wrapf-empty",
errutil.Wrapf(goErr.New("woo"), emptyString),
`woo`, `
error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- woo`,
},

{"wrapf-empty-arg",
errutil.Wrapf(goErr.New("woo"), emptyString, 123),
`%!(EXTRA int=123): woo`, `
error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- error with embedded safe details:
-- arg 1: <int>
- %!(EXTRA int=123):
- woo`,
},

{"handled assert",
errutil.HandleAsAssertionFailure(&werrFmt{goErr.New("woo"), "wuu"}),
`wuu: woo`,
`
assertion failure
- error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- wuu: woo:
original cause behind barrier: wuu:
-- verbose wrapper:
wuu
- woo`,
},

{"assert + wrap",
errutil.NewAssertionErrorWithWrappedErrf(&werrFmt{goErr.New("woo"), "wuu"}, "waa: %s", "hello"),
`waa: hello: wuu: woo`, `
Expand All @@ -80,8 +156,46 @@ assertion failure
-- arg 1: <string>
- waa: hello:
- wuu: woo:
original cause behind barrier:
wuu:
original cause behind barrier: wuu:
-- verbose wrapper:
wuu
- woo`,
},

{"assert + wrap empty",
errutil.NewAssertionErrorWithWrappedErrf(&werrFmt{goErr.New("woo"), "wuu"}, emptyString),
`wuu: woo`, `
assertion failure
- error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- wuu: woo:
original cause behind barrier: wuu:
-- verbose wrapper:
wuu
- woo`,
},

{"assert + wrap empty+arg",
errutil.NewAssertionErrorWithWrappedErrf(&werrFmt{goErr.New("woo"), "wuu"}, emptyString, 123),
`%!(EXTRA int=123): wuu: woo`, `
assertion failure
- error with attached stack trace:
github.com/cockroachdb/errors/errutil_test.TestFormat
<tab><path>
testing.tRunner
<tab><path>
runtime.goexit
<tab><path>
- error with embedded safe details:
-- arg 1: <int>
- %!(EXTRA int=123):
- wuu: woo:
original cause behind barrier: wuu:
-- verbose wrapper:
wuu
- woo`,
Expand Down Expand Up @@ -131,3 +245,5 @@ func (e *werrFmt) FormatError(p errbase.Printer) error {
}
return e.cause
}

var emptyString = ""
8 changes: 5 additions & 3 deletions errutil/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func Newf(format string, args ...interface{}) error {
// See the doc of `New()` for more details.
func NewWithDepthf(depth int, format string, args ...interface{}) error {
err := fmt.Errorf(format, args...)
err = safedetails.WithSafeDetails(err, format, args...)
if format != "" || len(args) > 0 {
err = safedetails.WithSafeDetails(err, format, args...)
}
err = withstack.WithStackDepth(err, 1+depth)
return err
}
Expand Down Expand Up @@ -100,10 +102,10 @@ func Wrapf(err error, format string, args ...interface{}) error {
// trace is configurable.
// The the doc of `Wrapf()` for more details.
func WrapWithDepthf(depth int, err error, format string, args ...interface{}) error {
if format != "" {
if format != "" || len(args) > 0 {
err = WithMessagef(err, format, args...)
err = safedetails.WithSafeDetails(err, format, args...)
}
err = safedetails.WithSafeDetails(err, format, args...)
err = withstack.WithStackDepth(err, depth+1)
return err
}
10 changes: 10 additions & 0 deletions errutil_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,15 @@ func NewAssertionErrorWithWrappedErrf(origErr error, format string, args ...inte
return errutil.NewAssertionErrorWithWrappedErrDepthf(1, origErr, format, args...)
}

// HandleAsAssertionFailure forwards a definition.
func HandleAsAssertionFailure(origErr error) error {
return errutil.HandleAsAssertionFailureDepth(1, origErr)
}

// HandleAsAssertionFailureDepth forwards a definition.
func HandleAsAssertionFailureDepth(depth int, origErr error) error {
return errutil.HandleAsAssertionFailureDepth(1+depth, origErr)
}

// As forwards a definition
func As(err error, target interface{}) bool { return errutil.As(err, target) }
13 changes: 13 additions & 0 deletions safedetails/safedetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ func TestFormat(t *testing.T) {
error with embedded safe details: a
- woo`},

{"safe empty",
safedetails.WithSafeDetails(baseErr, ""),
woo, `
safe detail wrapper with no details
- woo`},

{"safe nofmt+onearg",
safedetails.WithSafeDetails(baseErr, "", 123),
woo, `
error with embedded safe details:
-- arg 1: <int>
- woo`},

{"safe err",
safedetails.WithSafeDetails(baseErr, "a %v",
&os.PathError{
Expand Down
15 changes: 11 additions & 4 deletions safedetails/with_safedetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ var _ errbase.Formatter = (*withSafeDetails)(nil)
func (e *withSafeDetails) Format(s fmt.State, verb rune) { errbase.FormatError(e, s, verb) }

func (e *withSafeDetails) FormatError(p errbase.Printer) error {
if p.Detail() && len(e.safeDetails) > 0 {
p.Printf("error with embedded safe details: %s", e.safeDetails[0])
for _, d := range e.safeDetails[1:] {
p.Printf("\n%s", d)
if p.Detail() {
if len(e.safeDetails) == 0 || (len(e.safeDetails) == 1 && e.safeDetails[0] == "") {
p.Print("safe detail wrapper with no details")
} else {
p.Print("error with embedded safe details:")
if e.safeDetails[0] != "" {
p.Printf(" %s", e.safeDetails[0])
}
for _, d := range e.safeDetails[1:] {
p.Printf("\n%s", d)
}
}
}
return e.cause
Expand Down