package
0.2.14
Repository: https://github.com/projectdiscovery/utils.git
Documentation: pkg.go.dev

# README

errkit

why errkit when we already have errorutil ?


Introduced a year ago, errorutil aimed to capture error stacks for identifying deeply nested errors. However, its approach deviates from Go's error handling paradigm. In Go, libraries like "errors", "pkg/errors", and "uber.go/multierr" avoid using the .Error() method directly. Instead, they wrap errors with helper structs that implement specific interfaces, facilitating error chain traversal and the use of helper functions like .Cause() error or .Unwrap() error or errors.Is(). Contrarily, errorutil marshals errors to strings, which is incompatible with Go's error handling paradigm. Over time, the use of errorutil has become cumbersome due to its inability to replace any error package seamlessly and its lack of support for idiomatic error propagation or traversal in Go.

errkit is a new error library that addresses the shortcomings of errorutil. It offers the following features:

  • Seamless replacement for existing error packages, requiring no syntax changes or refactoring:
    • errors package
    • pkg/errors package (now deprecated)
    • uber/multierr package
  • errkit is compatible with all known Go error handling implementations. It can parse errors from any library and works with existing error handling libraries and helper functions like Is(), As(), Cause(), and more.
  • errkit is Go idiomatic and adheres to the Go error handling paradigm.
  • errkit supports attributes for structured error information or logging using slog.Attr (optional).
  • errkit implements and categorizes errors into different kinds, as detailed below.
    • ErrKindNetworkTemporary
    • ErrKindNetworkPermanent
    • ErrKindDeadline
    • Custom kinds via ErrKind interface
  • errkit provides helper functions for structured error logging using SlogAttrs and SlogAttrGroup.
  • errkit offers helper functions to implement public or user-facing errors by using error kinds interface.

Attributes Support

errkit supports optional error wrapping with attributes slog.Attr for structured error logging, providing a more organized approach to error logging than string wrapping.

// normal way of error propogating through nested stack
err := errkit.New("i/o timeout")

// xyz.go
err := errkit.Wrap(err,"failed to connect %s",addr)

// abc.go
err := errkit.Wrap(err,"error occured when downloading %s",xyz)

with attributes support you can do following

// normal way of error propogating through nested stack
err := errkit.New("i/o timeout")

// xyz.go
err = errkit.WithAttr(err,slog.Any("resource",domain))

// abc.go
err = errkit.WithAttr(err,slog.Any("action","download"))

Note

To keep errors concise and avoid unnecessary allocations, message wrapping and attributes count have a max depth set to 3. Adding more will not panic but will be simply ignored. This is configurable using the MAX_ERR_DEPTH env variable (default 3).

# Functions

Append appends given errors and returns a new error it ignores all nil errors.
Proxy to StdLib errors.As.
Cause returns the original error that caused this error.
Combine combines multiple errors into a single error.
CombineErrKinds combines multiple error kinds into a single error kind this is not recommended but available if needed It is currently used in ErrorX while printing the error It is recommended to implement a hierarchical error kind instead of using this outside of errkit.
Errors returns all underlying errors there were appended or joined.
FromError parses a given error to understand the error class and optionally adds given message for more info.
GetAllErrorKinds returns all error kinds from the error this should not be used unless very good reason to do so.
GetAttr returns all attributes of given error if it has any.
GetAttrValue returns the value of the attribute with given key.
GetErrorKind returns the first error kind from the error extra error kinds can be passed as optional arguments.
Proxy to StdLib errors.Is.
IsDeadlineErr checks if given error is a deadline error.
IsKind checks if given error is equal to one of the given errkind if error did not already have a kind, it tries to parse it using default error kinds and given kinds.
IsNetworkPermanentErr checks if given error is a permanent network error.
IsNetworkTemporaryErr checks if given error is a temporary network error.
Join joins given errors and returns a new error it ignores all nil errors Note: unlike Other libraries, Join does not use `\n` so it is equivalent to wrapping/Appending errors.
New creates a new error with the given message.
NewPrimitiveErrKind creates a new primitive error kind.
ToSlogAttrGroup returns a slog attribute group for the given error it is in format of: { "data": { "kind": "<error-kind>", "cause": "<cause>", "errors": [ <errs>..
ToSlogAttrs returns slog attributes for the given error it is in format of: { "kind": "<error-kind>", "cause": "<cause>", "errors": [ <errs>..
WithAttr wraps error with given attributes err = errkit.WithAttr(err,slog.Any("resource",domain)).
WithMessage.
WithMessagef.
Wrap wraps the given error with the message.
Wrapf wraps the given error with the message.

# Constants

DelimArrow is delim used by projectdiscovery/utils to join errors.
DelimArrowSerialized.
DelimMultiLine is delim used to join errors in multiline format.
DelimSemiColon is standard delim popularly used to join errors.
MultiLinePrefix is the prefix used for multiline errors.

# Variables

DefaultErrorKinds is the default error kinds used in classification if one intends to add more default error kinds it must be done in init() function of that package to avoid race conditions.
ErrKindDeadline indicates a timeout error in logical operations these are custom deadlines set by nuclei itself to prevent infinite hangs and in most cases are server side issues (ex: server connects but does not respond at all) a manual intervention is required.
ErrKindNetworkPermanent indicates a permanent error related to network operations these may not be resolved by retrying and need manual intervention ex: no address found for host.
ErrClassNetwork indicates an error related to network operations these may be resolved by retrying the operation with exponential backoff ex: Timeout awaiting headers, i/o timeout etc.
ErrKindUnknown indicates an unknown error class that has not been implemented yet this is used as fallback when converting a slog Item.
ErrorSeperator is the seperator used to join errors.
MaxErrorDepth is the maximum depth of errors to be unwrapped or maintained all errors beyond this depth will be ignored.

# Structs

ErrorX is a custom error type that can handle all known types of errors wrapping and joining strategies including custom ones and it supports error class which can be shown to client/users in more meaningful way.

# Interfaces

CauseError is implemented by errors that have a cause.
ComparableError is implemented by errors that can be compared.
ErrKind is an interface that represents a kind of error.
JoinedError is implemented by errors that are joined by Join.
WrappedError is implemented by errors that are wrapped.