-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy patherror.go
34 lines (30 loc) · 824 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
package internal
import (
"errors"
"fmt"
"runtime"
"strings"
"github.com/gjbae1212/go-wraperror"
)
var (
// ErrInvalidParams is an error type to use when passed arguments are invalid.
ErrInvalidParams = errors.New("[err] invalid params")
// ErrUnknown is an error type to use when error reason doesn't know.
ErrUnknown = errors.New("[err] unknown")
)
// WrapError wraps error.
func WrapError(err error) error {
if err != nil {
// Get program counter and line number
pc, _, line, _ := runtime.Caller(1)
// Get function name from program counter
fn := runtime.FuncForPC(pc).Name()
// Refine function name
details := strings.Split(fn, "/")
fn = details[len(details)-1]
// Build chain
chainErr := wraperror.Error(err)
return chainErr.Wrap(fmt.Errorf("[err][%s:%d]", fn, line))
}
return nil
}