Skip to content

Commit

Permalink
Make changes per review
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
StevenACoffman committed May 6, 2024
1 parent 557f993 commit 22ac828
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
18 changes: 11 additions & 7 deletions errtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func wrap(err error, callerPC uintptr) error {
}

// Format writes the return trace for given error to the writer.
// The output takes a fromat similar to the following:
// The output takes a format similar to the following:
//
// <error message>
//
Expand All @@ -79,6 +79,8 @@ func wrap(err error, callerPC uintptr) error {
// <file>:<line>
// [...]
//
// Any error that has a method `TracePC() uintptr` will
// contribute to the trace.
// If the error doesn't have a return trace attached to it,
// only the error message is reported.
// If the error is comprised of multiple errors (e.g. with [errors.Join]),
Expand All @@ -90,6 +92,8 @@ func Format(w io.Writer, target error) (err error) {
}

// FormatString writes the return trace for err to a string.
// Any error that has a method `TracePC() uintptr` will
// contribute to the trace.
// See [Format] for details of the output format.
func FormatString(target error) string {
var s strings.Builder
Expand Down Expand Up @@ -120,18 +124,18 @@ func (e *errTrace) Format(s fmt.State, verb rune) {
}

// TraceCall returns the program counter for the location in this frame.
func (e *errTrace) TraceCall() uintptr {
func (e *errTrace) TracePC() uintptr {
return e.pc
}

// TraceCaller is a provider of the Program Counter
// tracePCprovider is a provider of the Program Counter
// that the error originated with.
// The returned PC is intended to be used with
// runtime.CallersFrames or runtime.FuncForPC
// to aid in generating the error return trace
type TraceCaller interface {
TraceCall() uintptr
type tracePCprovider interface {
TracePC() uintptr
}

// compile time TraceCaller interface check
var _ TraceCaller = &errTrace{}
// compile time tracePCprovider interface check
var _ tracePCprovider = &errTrace{}
10 changes: 5 additions & 5 deletions unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import "runtime"
//
// You can use this for structured access to trace information.
func UnwrapFrame(err error) (frame runtime.Frame, inner error, ok bool) { //nolint:revive // error is intentionally middle return
e, ok := err.(TraceCaller)
e, ok := err.(tracePCprovider)
if !ok {
return runtime.Frame{}, err, false
}

wrapErr := UnwrapOnce(err)
frames := runtime.CallersFrames([]uintptr{e.TraceCall()})
wrapErr := unwrapOnce(err)
frames := runtime.CallersFrames([]uintptr{e.TracePC()})
f, _ := frames.Next()
if f == (runtime.Frame{}) {
// Unlikely, but if PC didn't yield a frame,
Expand All @@ -26,13 +26,13 @@ func UnwrapFrame(err error) (frame runtime.Frame, inner error, ok bool) { //noli
return f, wrapErr, true
}

// UnwrapOnce accesses the direct cause of the error if any, otherwise
// unwrapOnce accesses the direct cause of the error if any, otherwise
// returns nil.
//
// It supports both errors implementing causer (`Cause()` method, from
// github.com/pkg/errors) and `Wrapper` (`Unwrap()` method, from the
// Go 2 error proposal).
func UnwrapOnce(err error) error {
func unwrapOnce(err error) error {
switch e := err.(type) {
case interface{ Cause() error }:
return e.Cause()
Expand Down
6 changes: 3 additions & 3 deletions unwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ func TestUnwrapOnce(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := UnwrapOnce(tt.arg)
err := unwrapOnce(tt.arg)
if (err != nil) != tt.want.wantErr {
t.Errorf("UnwrapOnce() error = %v, but wantErr %v", err, tt.want.wantErr)
t.Errorf("unwrapOnce() error = %v, but wantErr %v", err, tt.want.wantErr)
} else if !errors.Is(err, tt.want.matchErr) {
t.Errorf("UnwrapOnce() error = %v, does not match matchErr %v", err, tt.want.matchErr)
t.Errorf("unwrapOnce() error = %v, does not match matchErr %v", err, tt.want.matchErr)
}
})
}
Expand Down

0 comments on commit 22ac828

Please sign in to comment.