Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions channelutil/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"sync"

errorutil "github.com/projectdiscovery/utils/errors"
"github.com/projectdiscovery/utils/errkit"
)

// CloneOptions provides options for Cloning channels
Expand Down Expand Up @@ -43,13 +43,13 @@ func NewCloneChannels[T any](opts *CloneOptions) *CloneChannels[T] {
// Clone takes data from source channel(src) and sends them to sinks(send only channel) without being totally unfair
func (s *CloneChannels[T]) Clone(ctx context.Context, src chan T, sinks ...chan<- T) error {
if src == nil {
return errorutil.New("source channel is nil").WithTag("Clone", "channel")
return errkit.New("source channel is nil")
}

// check if all sinks are not nil
for _, ch := range sinks {
if ch == nil {
return errorutil.New("nil sink found").WithTag("Clone", "channel")
return errkit.New("nil sink found")
}
}

Expand Down
8 changes: 4 additions & 4 deletions channelutil/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"sync"

errorutil "github.com/projectdiscovery/utils/errors"
"github.com/projectdiscovery/utils/errkit"
)

// JoinChannels provides method to Join channels
Expand All @@ -20,14 +20,14 @@ type JoinChannels[T any] struct {
// JoinChannels Joins Many Channels to Create One
func (j *JoinChannels[T]) Join(ctx context.Context, sink chan T, sources ...<-chan T) error {
if sink == nil {
return errorutil.New("sink cannot be nil").WithTag("join", "channel")
return errkit.New("sink cannot be nil")
}
if len(sources) == 0 {
return errorutil.New("sources cannot be zero").WithTag("join", "channel")
return errkit.New("sources cannot be zero")
}
for _, v := range sources {
if v == nil {
return errorutil.New("given source is nil").WithTag("join", "channel")
return errkit.New("given source is nil")
}
}

Expand Down
58 changes: 38 additions & 20 deletions errkit/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
// EnableTimestamp controls whether error timestamps are included
EnableTimestamp = env.GetEnvOrDefault("ENABLE_ERR_TIMESTAMP", false)
// EnableTrace controls whether error stack traces are included
EnableTrace = env.GetEnvOrDefault("ENABLE_ERR_TRACE", false)
EnableTrace = env.GetEnvOrDefault("ERRKIT_ENABLE_TRACE", false)
)

// ErrorX is a custom error type that can handle all known types of errors
Expand Down Expand Up @@ -220,19 +220,18 @@ func FromError(err error) *ErrorX {
return nucleiErr
}

// New creates a new error with the given message
// it follows slog pattern of adding and expects in the same way
// New creates a new error with the given message and slog attributes
//
// Example:
//
// this is correct (√)
// errkit.New("this is a nuclei error","address",host)
// errkit.New("connection failed", "address", host, "port", port)
//
// this is not readable/recommended (x)
// errkit.New("this is a nuclei error",slog.String("address",host))
// this is also correct (√)
// errkit.New("timeout occurred")
//
// this is wrong (x)
// errkit.New("this is a nuclei error %s",host)
// this is not recommended (x) - use Newf instead
// errkit.New("error on host %s", host)
func New(msg string, args ...interface{}) *ErrorX {
e := &ErrorX{}
e.init()
Expand All @@ -243,27 +242,46 @@ func New(msg string, args ...interface{}) *ErrorX {
return e
}

// Msgf adds a message to the error
// it follows slog pattern of adding and expects in the same way
// Newf creates a new error with a formatted message
//
// Example:
//
// this is correct (√)
// myError.Msgf("dial error","network","tcp")
// errkit.Newf("connection failed on %s:%d", host, port)
func Newf(format string, args ...interface{}) *ErrorX {
e := &ErrorX{}
e.init()
msg := fmt.Sprintf(format, args...)
e.append(errors.New(msg))
return e
}

// Msg adds a plain message to the error
//
// this is not readable/recommended (x)
// myError.Msgf(slog.String("address",host))
// Example:
//
// this is wrong (x)
// myError.Msgf("this is a nuclei error %s",host)
func (e *ErrorX) Msgf(format string, args ...interface{}) {
// myError.Msg("connection failed")
func (e *ErrorX) Msg(message string) {
if e == nil {
return
}
if len(args) == 0 {
e.append(errors.New(format))
e.append(errors.New(message))
}

// Msgf adds a formatted message to the error
//
// Example:
//
// this is correct (√)
// myError.Msgf("dial error on %s:%d", host, port)
//
// this is also correct (√)
// myError.Msgf("connection failed")
func (e *ErrorX) Msgf(format string, args ...interface{}) {
if e == nil {
return
}
e.append(fmt.Errorf(format, args...))
msg := fmt.Sprintf(format, args...)
e.append(errors.New(msg))
}

// SetClass sets the class of the error
Expand Down
18 changes: 9 additions & 9 deletions errkit/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/pkg/errors"
errorutil "github.com/projectdiscovery/utils/errors"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"

Expand Down Expand Up @@ -71,14 +70,15 @@ func TestErrorIs(t *testing.T) {
}
}

func TestErrorUtil(t *testing.T) {
utilErr := errorutil.New("got err while executing http://206.189.19.240:8000/wp-content/plugins/wp-automatic/inc/csv.php <- POST http://206.189.19.240:8000/wp-content/plugins/wp-automatic/inc/csv.php giving up after 2 attempts: Post \"http://206.189.19.240:8000/wp-content/plugins/wp-automatic/inc/csv.php\": [:RUNTIME] ztls fallback failed <- dial tcp 206.189.19.240:8000: connect: connection refused")
x := ErrorX{}
parseError(&x, utilErr)
if len(x.errs) != 3 {
t.Fatal("expected 3 errors")
}
}
// TestErrorUtil commented out to avoid circular dependency with errorutil
// func TestErrorUtil(t *testing.T) {
// utilErr := errorutil.New("got err while executing http://206.189.19.240:8000/wp-content/plugins/wp-automatic/inc/csv.php <- POST http://206.189.19.240:8000/wp-content/plugins/wp-automatic/inc/csv.php giving up after 2 attempts: Post \"http://206.189.19.240:8000/wp-content/plugins/wp-automatic/inc/csv.php\": [:RUNTIME] ztls fallback failed <- dial tcp 206.189.19.240:8000: connect: connection refused")
// x := ErrorX{}
// parseError(&x, utilErr)
// if len(x.errs) != 3 {
// t.Fatal("expected 3 errors")
// }
// }

func TestErrKindCheck(t *testing.T) {
x := New("port closed or filtered").SetKind(ErrKindNetworkPermanent)
Expand Down
4 changes: 2 additions & 2 deletions errkit/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Wrap(err error, message string) error {
}
x := &ErrorX{}
parseError(x, err)
x.Msgf("%s", message)
x.Msg(message)
return x
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func WithMessage(err error, message string) error {
}
x := &ErrorX{}
parseError(x, err)
x.Msgf("%s", message)
x.Msg(message)
return x
}

Expand Down
Loading
Loading