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

Commit a7f2be0

Browse files
committed
added godoc
1 parent e41b26d commit a7f2be0

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

errors.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,41 @@ import (
66
"io"
77
"os"
88
"runtime"
9+
"strings"
910
)
1011

12+
type loc uintptr
13+
14+
func (l loc) Location() (string, int) {
15+
pc := uintptr(l)
16+
fn := runtime.FuncForPC(pc)
17+
if fn == nil {
18+
return "unknown", 0
19+
}
20+
21+
_, prefix, _, _ := runtime.Caller(0)
22+
file, line := fn.FileLine(pc)
23+
if i := strings.LastIndex(prefix, "github.com/pkg/errors"); i > 0 {
24+
file = file[i:]
25+
}
26+
return file, line
27+
}
28+
1129
// New returns an error that formats as the given text.
1230
func New(text string) error {
1331
return struct {
1432
error
15-
pc uintptr
33+
loc
1634
}{
1735
fmt.Errorf(text),
18-
pc(),
36+
loc(pc()),
1937
}
2038
}
2139

2240
type e struct {
2341
cause error
2442
message string
25-
pc uintptr
43+
loc
2644
}
2745

2846
func (e *e) Error() string {
@@ -42,7 +60,7 @@ func Wrap(cause error, message string) error {
4260
return &e{
4361
cause: cause,
4462
message: message,
45-
pc: pc(),
63+
loc: loc(pc()),
4664
}
4765
}
4866

@@ -77,6 +95,15 @@ type locationer interface {
7795
}
7896

7997
// Print prints the error to Stderr.
98+
// If the error implements the Causer interface described in Cause
99+
// Print will recurse into the error's cause.
100+
// If the error implements the inteface:
101+
//
102+
// type Location interface {
103+
// Location() (file string, line int)
104+
// }
105+
//
106+
// Print will also print the file and line of the error.
80107
func Print(err error) {
81108
Fprint(os.Stderr, err)
82109
}
@@ -89,7 +116,7 @@ func Fprint(w io.Writer, err error) {
89116
location, ok := err.(locationer)
90117
if ok {
91118
file, line := location.Location()
92-
fmt.Fprint(w, "%s:%d: ", file, line)
119+
fmt.Fprintf(w, "%s:%d: ", file, line)
93120
}
94121
switch err := err.(type) {
95122
case *e:

errors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ func TestFprint(t *testing.T) {
124124
want: "error\n",
125125
}, {
126126
err: Wrap(x, "message"),
127-
want: "message\nerror\n",
127+
want: "github.com/pkg/errors/errors_test.go:126: message\nerror\n",
128128
}, {
129129
err: Wrap(Wrap(x, "message"), "another message"),
130-
want: "another message\nmessage\nerror\n",
130+
want: "github.com/pkg/errors/errors_test.go:129: another message\ngithub.com/pkg/errors/errors_test.go:129: message\nerror\n",
131131
}}
132132

133133
for i, tt := range tests {

example_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ func ExampleWrap() {
2323
}
2424

2525
func fn() error {
26-
return errors.Wrap(errors.Wrap(errors.Wrap(errors.New("error"), "inner"), "middle"), "outer")
26+
e1 := errors.New("error")
27+
e2 := errors.Wrap(e1, "inner")
28+
e3 := errors.Wrap(e2, "middle")
29+
return errors.Wrap(e3, "outer")
2730
}
2831

2932
func ExampleCause() {
@@ -39,8 +42,8 @@ func ExampleFprint() {
3942
err := fn()
4043
errors.Fprint(os.Stdout, err)
4144

42-
// Output: outer
43-
// middle
44-
// inner
45+
// Output: github.com/pkg/errors/example_test.go:29: outer
46+
// github.com/pkg/errors/example_test.go:28: middle
47+
// github.com/pkg/errors/example_test.go:27: inner
4548
// error
4649
}

0 commit comments

Comments
 (0)