Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit daa1017

Browse files
committed
Refactor errors.location to be a stack of callers (#25)
Refactory errors.location to be a stack of callers rather than the topmost caller. This is in perparation for storing the entire error stack inside topmost error.
1 parent d814416 commit daa1017

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

errors.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ import (
5555
"strings"
5656
)
5757

58-
// location represents a program counter that
59-
// implements the Location() method.
60-
type location uintptr
58+
// location represents a stack of programm counters.
59+
type location []uintptr
6160

6261
func (l location) Location() (string, int) {
63-
pc := uintptr(l) - 1
62+
pc := l[0] - 1
6463
fn := runtime.FuncForPC(pc)
6564
if fn == nil {
6665
return "unknown", 0
@@ -110,13 +109,12 @@ func (l location) Location() (string, int) {
110109

111110
// New returns an error that formats as the given text.
112111
func New(text string) error {
113-
pc, _, _, _ := runtime.Caller(1)
114112
return struct {
115113
error
116114
location
117115
}{
118116
errors.New(text),
119-
location(pc),
117+
caller(),
120118
}
121119
}
122120

@@ -132,13 +130,12 @@ func (c cause) Message() string { return c.message }
132130
// Errorf formats according to a format specifier and returns the string
133131
// as a value that satisfies error.
134132
func Errorf(format string, args ...interface{}) error {
135-
pc, _, _, _ := runtime.Caller(1)
136133
return struct {
137134
error
138135
location
139136
}{
140137
fmt.Errorf(format, args...),
141-
location(pc),
138+
caller(),
142139
}
143140
}
144141

@@ -148,8 +145,7 @@ func Wrap(cause error, message string) error {
148145
if cause == nil {
149146
return nil
150147
}
151-
pc, _, _, _ := runtime.Caller(1)
152-
return wrap(cause, message, pc)
148+
return wrap(cause, message, caller())
153149
}
154150

155151
// Wrapf returns an error annotating the cause with the format specifier.
@@ -158,11 +154,10 @@ func Wrapf(cause error, format string, args ...interface{}) error {
158154
if cause == nil {
159155
return nil
160156
}
161-
pc, _, _, _ := runtime.Caller(1)
162-
return wrap(cause, fmt.Sprintf(format, args...), pc)
157+
return wrap(cause, fmt.Sprintf(format, args...), caller())
163158
}
164159

165-
func wrap(err error, msg string, pc uintptr) error {
160+
func wrap(err error, msg string, loc location) error {
166161
return struct {
167162
cause
168163
location
@@ -171,7 +166,7 @@ func wrap(err error, msg string, pc uintptr) error {
171166
cause: err,
172167
message: msg,
173168
},
174-
location(pc),
169+
loc,
175170
}
176171
}
177172

@@ -239,3 +234,9 @@ func Fprint(w io.Writer, err error) {
239234
err = cause.Cause()
240235
}
241236
}
237+
238+
func caller() location {
239+
var pcs [1]uintptr
240+
n := runtime.Callers(3, pcs[:])
241+
return location(pcs[0:n])
242+
}

0 commit comments

Comments
 (0)