11package errorutil_test
22
33import (
4- "fmt "
4+ "errors "
55 "strings"
66 "testing"
77
8- errors "github.com/projectdiscovery/utils/errors"
8+ errorutil "github.com/projectdiscovery/utils/errors"
99)
1010
11+ type customError struct {
12+ msg string
13+ }
14+
15+ func (c * customError ) Error () string {
16+ return c .msg
17+ }
18+
1119func TestErrorEqual (t * testing.T ) {
12- err1 := fmt . Errorf ("error init x" )
13- err2 := errors .NewWithErr (err1 )
14- err3 := errors .NewWithTag ("testing" , "error init" )
20+ err1 := errors . New ("error init x" )
21+ err2 := errorutil .NewWithErr (err1 )
22+ err3 := errorutil .NewWithTag ("testing" , "error init" )
1523 var errnil error
1624
17- if ! errors .IsAny (err1 , err2 , errnil ) {
25+ if ! errorutil .IsAny (err1 , err2 , errnil ) {
1826 t .Errorf ("expected errors to be equal" )
1927 }
20- if errors .IsAny (err1 , err3 , errnil ) {
28+ if errorutil .IsAny (err1 , err3 , errnil ) {
2129 t .Errorf ("expected error to be not equal" )
2230 }
2331}
2432
2533func TestWrapWithNil (t * testing.T ) {
26- err1 := errors .NewWithTag ("niltest" , "non nil error" ).WithLevel (errors .Fatal )
34+ err1 := errorutil .NewWithTag ("niltest" , "non nil error" ).WithLevel (errorutil .Fatal )
2735 var errx error
2836
29- if errors .WrapwithNil (errx , err1 ) != nil {
37+ if errorutil .WrapwithNil (errx , err1 ) != nil {
3038 t .Errorf ("when base error is nil " )
3139 }
3240}
3341
3442func TestStackTrace (t * testing.T ) {
35- err := errors .New ("base error" )
43+ err := errorutil .New ("base error" )
3644 relay := func (err error ) error {
3745 return err
3846 }
@@ -42,7 +50,7 @@ func TestStackTrace(t *testing.T) {
4250 if strings .Contains (errx .Error (), "captureStack" ) {
4351 t .Errorf ("stacktrace should be disabled by default" )
4452 }
45- errors .ShowStackTrace = true
53+ errorutil .ShowStackTrace = true
4654 if ! strings .Contains (errx .Error (), "captureStack" ) {
4755 t .Errorf ("missing stacktrace got %v" , errx .Error ())
4856 }
@@ -52,8 +60,8 @@ func TestStackTrace(t *testing.T) {
5260func TestErrorCallback (t * testing.T ) {
5361 callbackExecuted := false
5462
55- err := errors .NewWithTag ("callback" , "got error" ).WithCallback (func (level errors .ErrorLevel , err string , tags ... string ) {
56- if level != errors .Runtime {
63+ err := errorutil .NewWithTag ("callback" , "got error" ).WithCallback (func (level errorutil .ErrorLevel , err string , tags ... string ) {
64+ if level != errorutil .Runtime {
5765 t .Errorf ("Default error level should be Runtime" )
5866 }
5967 if tags [0 ] != "callback" {
@@ -72,3 +80,62 @@ func TestErrorCallback(t *testing.T) {
7280 t .Errorf ("error callback failed to execute" )
7381 }
7482}
83+
84+ func TestErrorIs (t * testing.T ) {
85+ var ErrTest = errors .New ("test error" )
86+
87+ err := errorutil .NewWithErr (ErrTest ).Msgf ("message %s" , "test" )
88+
89+ if ! errors .Is (err , ErrTest ) {
90+ t .Errorf ("expected error to match ErrTest" )
91+ }
92+ }
93+
94+ func TestUnwrap (t * testing.T ) {
95+ // Test basic unwrapping
96+ baseErr := errors .New ("base error" )
97+ wrappedErr := errorutil .NewWithErr (baseErr )
98+
99+ if ! errors .Is (wrappedErr , baseErr ) {
100+ t .Errorf ("expected wrapped error to match base error" )
101+ }
102+
103+ // Test unwrapping thru error chain
104+ middleErr := errorutil .NewWithErr (baseErr ).WithTag ("middle" )
105+ topErr := errorutil .NewWithErr (middleErr ).WithTag ("top" )
106+
107+ if ! errors .Is (topErr , baseErr ) {
108+ t .Errorf ("expected topErr to match baseErr through chain" )
109+ }
110+
111+ if ! errors .Is (topErr , middleErr ) {
112+ t .Errorf ("expected topErr to match middleErr" )
113+ }
114+
115+ // Test direct unwrap method
116+ if unwrapped := errors .Unwrap (wrappedErr ); unwrapped != baseErr {
117+ t .Errorf ("expected direct unwrap to return baseErr, got %v" , unwrapped )
118+ }
119+
120+ // Test unwrapping with Wrap method
121+ err1 := errors .New ("first error" )
122+ err2 := errors .New ("second error" )
123+ combined := errorutil .New ("combined error" ).Wrap (err1 , err2 )
124+
125+ if ! errors .Is (combined , err1 ) {
126+ t .Errorf ("expected combined error to match err1" )
127+ }
128+
129+ // Test errors.As functionality
130+ customErr := & customError {msg : "custom error" }
131+ wrappedCustom := errorutil .NewWithErr (customErr ).WithTag ("wrapped" )
132+
133+ var targetCustom * customError
134+ if ! errors .As (wrappedCustom , & targetCustom ) {
135+ t .Errorf ("expected errors.As to find custom error type" )
136+ }
137+
138+ if targetCustom .msg != "custom error" {
139+ t .Errorf ("expected custom error message 'custom error', got %s" , targetCustom .msg )
140+ }
141+ }
0 commit comments