forked from go-errors/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreation.go
More file actions
57 lines (50 loc) · 1.75 KB
/
Copy pathcreation.go
File metadata and controls
57 lines (50 loc) · 1.75 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
package errors
import (
"fmt"
"runtime"
)
// New makes an Error from the given value. If that value is already an
// error then it will be used directly, if not, it will be passed to
// fmt.Errorf("%v"). The stacktrace will point to the line of code that
// called New.
func New(e interface{}) *Err {
return Wrap(e, 1)
}
// Wrap makes an Error from the given value. If that value is already an
// error then it will be used directly, if not, it will be passed to
// fmt.Errorf("%v"). The skip parameter indicates how far up the stack
// to start the stacktrace. 0 is from the current call, 1 from its caller, etc.
func Wrap(e interface{}, skip int) *Err {
var err error
switch e := e.(type) {
case error:
err = e
case nil:
err = nil
default:
err = fmt.Errorf("%v", e)
}
stack := make([]uintptr, MaxStackDepth)
length := runtime.Callers(2+skip, stack[:])
return &Err{
Underlying: err,
stack: stack[:length],
}
}
// Wrapf makes an Error from the given value. If that value is already
// an error then it will be used directly as the underlying error, if not,
// it will be passed to fmt.Errorf("%v"). The prefixf parameter is used to
// add a formatted prefix to the error message when calling Error(). The skip
// pameter indicates how far up the stack to start the stacktrace; 0 is from
// the current call, 1 from its caller, etc.
func Wrapf(e interface{}, prefixf string, skip int, a ...interface{}) *Err {
err := Wrap(e, skip+1)
err.prefix = fmt.Sprintf(prefixf, a...)
return err
}
// Errorf creates a new error with the given message. You can use it
// as a drop-in replacement for fmt.Errorf() to provide descriptive
// errors in return values.
func Errorf(format string, a ...interface{}) *Err {
return Wrap(fmt.Errorf(format, a...), 1)
}