forked from heindl/caldav-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
37 lines (33 loc) · 751 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
package utils
import (
"fmt"
"reflect"
"runtime"
)
type Error struct {
method interface{}
message string
context interface{}
cause error
}
func NewError(method interface{}, message string, context interface{}, cause error) *Error {
e := new(Error)
e.method = method
e.message = message
e.context = context
e.cause = cause
return e
}
func (e *Error) Error() string {
pc := reflect.ValueOf(e.method).Pointer()
fn := runtime.FuncForPC(pc).Name()
msg := fmt.Sprintf("error: %s\nfunc: %s", e.message, fn)
if e.context != nil {
tname := reflect.ValueOf(e.context).Type()
msg = fmt.Sprintf("%s\ncontext: %s", msg, tname.String())
}
if e.cause != nil {
msg = fmt.Sprintf("%s\ncause: %s", msg, e.cause.Error())
}
return msg
}