@@ -3,21 +3,18 @@ package errorx
33import (
44 "errors"
55 "fmt"
6- "runtime"
7- "strings"
86
97 "github.com/imdario/mergo"
8+ "github.com/rizalgowandy/gdk/pkg/fn"
109)
1110
12- var ServiceName = "rizalgowandy"
11+ const callerSkip = 2
1312
1413// E for creating new error.
1514// error should always be the first param.
1615func E (args ... interface {}) error {
1716 if len (args ) == 0 {
18- _ , file , line , _ := runtime .Caller (1 )
19- file = file [strings .Index (file , ServiceName )+ len (ServiceName ):]
20- return Errorf ("errorx.E: bad call without args from file=%s:%d" , file , line )
17+ return Errorf ("errorx.E: bad call without args from file=%s" , fn .Line (callerSkip ))
2118 }
2219
2320 e := & Error {}
@@ -27,18 +24,17 @@ func E(args ...interface{}) error {
2724 // Copy and put the errors back.
2825 errCopy := * arg
2926 e = & errCopy
27+ e .OpTraces = append ([]Op {Op (fn .Name (callerSkip ))}, e .OpTraces ... )
3028
3129 case error :
3230 e .Err = arg
33- _ , file , line , _ := runtime .Caller (1 )
34- file = file [strings .Index (file , ServiceName )+ len (ServiceName ):]
35- e .Line = Line (fmt .Sprintf ("%s:%d" , file , line ))
31+ e .Line = Line (fn .Line (callerSkip ))
32+ e .OpTraces = append ([]Op {Op (fn .Name (callerSkip ))}, e .OpTraces ... )
3633
3734 case string :
3835 e .Err = Errorf (arg )
39- _ , file , line , _ := runtime .Caller (1 )
40- file = file [strings .Index (file , ServiceName )+ len (ServiceName ):]
41- e .Line = Line (fmt .Sprintf ("%s:%d" , file , line ))
36+ e .Line = Line (fn .Line (callerSkip ))
37+ e .OpTraces = append ([]Op {Op (fn .Name (callerSkip ))}, e .OpTraces ... )
4238
4339 case Code :
4440 // New code will always replace the old code.
@@ -60,7 +56,8 @@ func E(args ...interface{}) error {
6056 }
6157
6258 case Op :
63- e .OpTraces = append ([]Op {arg }, e .OpTraces ... )
59+ // For backward compatibility.
60+ // Client is no longer to pass Op manually as an argument but will be filled automatically.
6461
6562 case Message :
6663 e .Message = arg
@@ -70,9 +67,7 @@ func E(args ...interface{}) error {
7067
7168 default :
7269 // The default error is unknown.
73- _ , file , line , _ := runtime .Caller (1 )
74- file = file [strings .Index (file , ServiceName )+ len (ServiceName ):]
75- msg := fmt .Sprintf ("errorx.E: bad call from file=%s:%d args=%v" , file , line , args )
70+ msg := fmt .Sprintf ("errorx.E: bad call from file=%s args=%v" , fn .Line (callerSkip ), args )
7671 return Errorf (msg + "; unknown_type=%T value=%v" , arg , arg )
7772 }
7873 }
@@ -118,7 +113,7 @@ func Match(errs1, errs2 error) bool {
118113
119114// Is reports whether err is an *Error of the given Code.
120115// If err is nil then Is returns false.
121- func Is (code Code , err error ) bool {
116+ func Is (err error , code Code ) bool {
122117 if err == nil {
123118 return false
124119 }
@@ -133,7 +128,7 @@ func Is(code Code, err error) bool {
133128 }
134129
135130 if e .Err != nil {
136- return Is (code , e .Err )
131+ return Is (e .Err , code )
137132 }
138133
139134 return false
0 commit comments