-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
44 lines (38 loc) · 998 Bytes
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package goerr
import (
"fmt"
)
// Error is an error object that stores stack frame information,
// create new instances with `New()`.
type Error struct {
message string
innerErr error
caller uintptr
}
// New is the constructor for the `Error` object.
//
// It will accept any value and convert it to an error using
// `fmt.Errorf("%v", value)` if need be and then set the
// resulting value as the innerErr.
func New(value interface{}) *Error {
err, ok := value.(error)
if !ok {
err = fmt.Errorf("%+v", value)
}
return &Error{innerErr: err}
}
// Error implements the stdlib error interface.
func (g *Error) Error() string {
if g.message == "" {
return g.innerErr.Error()
}
return fmt.Sprintf("%s: %s", g.message, g.innerErr.Error())
}
// Unwrap implements the stdlib error interface.
func (g *Error) Unwrap() error {
return g.innerErr
}
// Frame returns the stack frame object attached to this error.
func (g *Error) Frame() *StackFrame {
return NewStackFrame(g.caller)
}