@@ -56,9 +56,11 @@ import (
5656 "strings"
5757)
5858
59- type loc uintptr
59+ // location represents a program counter that
60+ // implements the Location() method.
61+ type location uintptr
6062
61- func (l loc ) Location () (string , int ) {
63+ func (l location ) Location () (string , int ) {
6264 pc := uintptr (l ) - 1
6365 fn := runtime .FuncForPC (pc )
6466 if fn == nil {
@@ -112,26 +114,21 @@ func New(text string) error {
112114 pc , _ , _ , _ := runtime .Caller (1 )
113115 return struct {
114116 error
115- loc
117+ location
116118 }{
117119 errors .New (text ),
118- loc (pc ),
120+ location (pc ),
119121 }
120122}
121123
122- type e struct {
124+ type cause struct {
123125 cause error
124126 message string
125- loc
126127}
127128
128- func (e * e ) Error () string {
129- return e .message + ": " + e .cause .Error ()
130- }
131-
132- func (e * e ) Cause () error {
133- return e .cause
134- }
129+ func (c cause ) Error () string { return c .Message () + ": " + c .Cause ().Error () }
130+ func (c cause ) Cause () error { return c .cause }
131+ func (c cause ) Message () string { return c .message }
135132
136133// Wrap returns an error annotating the cause with message.
137134// If cause is nil, Wrap returns nil.
@@ -140,11 +137,7 @@ func Wrap(cause error, message string) error {
140137 return nil
141138 }
142139 pc , _ , _ , _ := runtime .Caller (1 )
143- return & e {
144- cause : cause ,
145- message : message ,
146- loc : loc (pc ),
147- }
140+ return wrap (cause , message , pc )
148141}
149142
150143// Wrapf returns an error annotating the cause with the format specifier.
@@ -154,10 +147,19 @@ func Wrapf(cause error, format string, args ...interface{}) error {
154147 return nil
155148 }
156149 pc , _ , _ , _ := runtime .Caller (1 )
157- return & e {
158- cause : cause ,
159- message : fmt .Sprintf (format , args ... ),
160- loc : loc (pc ),
150+ return wrap (cause , fmt .Sprintf (format , args ... ), pc )
151+ }
152+
153+ func wrap (err error , msg string , pc uintptr ) error {
154+ return struct {
155+ cause
156+ location
157+ }{
158+ cause {
159+ cause : err ,
160+ message : msg ,
161+ },
162+ location (pc ),
161163 }
162164}
163165
@@ -187,10 +189,6 @@ func Cause(err error) error {
187189 return err
188190}
189191
190- type locationer interface {
191- Location () (string , int )
192- }
193-
194192// Print prints the error to Stderr.
195193// If the error implements the Causer interface described in Cause
196194// Print will recurse into the error's cause.
@@ -209,15 +207,21 @@ func Print(err error) {
209207// The format of the output is the same as Print.
210208// If err is nil, nothing is printed.
211209func Fprint (w io.Writer , err error ) {
210+ type location interface {
211+ Location () (string , int )
212+ }
213+ type message interface {
214+ Message () string
215+ }
216+
212217 for err != nil {
213- location , ok := err .(locationer )
214- if ok {
215- file , line := location .Location ()
218+ if err , ok := err .(location ); ok {
219+ file , line := err .Location ()
216220 fmt .Fprintf (w , "%s:%d: " , file , line )
217221 }
218222 switch err := err .(type ) {
219- case * e :
220- fmt .Fprintln (w , err .message )
223+ case message :
224+ fmt .Fprintln (w , err .Message () )
221225 default :
222226 fmt .Fprintln (w , err .Error ())
223227 }
0 commit comments