| 1 |
package errors |
| 2 |
|
| 3 |
import "runtime" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type errorInfo struct { |
| 12 |
|
| 13 |
|
| 14 |
message string |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
cause error |
| 19 |
|
| 20 |
|
| 21 |
underlying error |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
callerPC [2]uintptr |
| 28 |
} |
| 29 |
|
| 30 |
func newError(underlying, cause error, msg string) error { |
| 31 |
err := &errorInfo{ |
| 32 |
underlying: underlying, |
| 33 |
cause: cause, |
| 34 |
message: msg, |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
err.setLocation(2) |
| 39 |
return err |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func (e *errorInfo) Underlying() error { |
| 44 |
return e.underlying |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func (e *errorInfo) Cause() error { |
| 49 |
return e.cause |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (e *errorInfo) Message() string { |
| 54 |
return e.message |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (e *errorInfo) Error() string { |
| 59 |
switch { |
| 60 |
case e.message == "" && e.underlying == nil: |
| 61 |
return "<no error>" |
| 62 |
case e.message == "": |
| 63 |
return e.underlying.Error() |
| 64 |
case e.underlying == nil: |
| 65 |
return e.message |
| 66 |
} |
| 67 |
return e.message + ": " + e.underlying.Error() |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func (e *errorInfo) GoString() string { |
| 73 |
return Details(e) |
| 74 |
} |
| 75 |
|
| 76 |
func (e *errorInfo) setLocation(callDepth int) { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
var callerPC [2]uintptr |
| 85 |
if runtime.Callers(callDepth+2, callerPC[:]) == len(callerPC) { |
| 86 |
e.callerPC = callerPC |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (e *errorInfo) Location() (file string, line int) { |
| 92 |
frames := runtime.CallersFrames(e.callerPC[:]) |
| 93 |
frame, ok := frames.Next() |
| 94 |
if ok { |
| 95 |
return frame.File, frame.Line |
| 96 |
} |
| 97 |
return "", 0 |
| 98 |
} |