Categorygithub.com/darwayne/errutil
modulepackage
1.7.0
Repository: https://github.com/darwayne/errutil.git
Documentation: pkg.go.dev

# README

Go Report Card GoDoc license

errutil

Package errutil provides simple error utilities predominantly around behavioral errors and panicked errors

Using Error as a constant

const myErr errutil.StringErr = "ruh oh"
....

if err == "ruh oh" {
	// since errutil.StringErr is a string you can compare with other strings
	// and save the error as a constant
	return "this is fine"
}

Adding Common Behavior To An Error

func myFunc() error {
    const myErr errutil.StringErr = "ruh oh"
    return errutil.Wrap(myErr, errutil.WithNotFound(), errutil.WithConflict())
}

func myFunc2() error {
    return errutil.New("ruh oh", errutil.WithEasyTags(
		"example", "yes",
		"component", "main",
		))
}

func main(){
	err := myFunc()
	if err != nil && errutil.IsNotFound(err) || errutil.IsConflict(err) {
	    fmt.Println("not found or conflict behavior detected .. handling")	
   }

   err = myFunc2()
	if errutil.IsTaggable(err) {
		fmt.Printf("error tags are: %+v", errutil.GetTags(err))
	}
}

Handling Panicked Errors

If you find your code is like the following with multiple error returns consider using the ExpectedPanicAsError function

Code with pass through error returns

func myFunc() error {
	if err := a1(); err != nil {
		return err
    }

    if err := a2(); err != nil {
        return err
    }

    if err := a3(); err != nil {
        return err
    }
}

Code with panics

func myFunc() (e error) {
	defer errutil.ExpectedPanicAsError(&e)
	a1()
	a2()
	a3();
}

func a1(){
	panic(errors.New("notice we are panicking with an error interface"))
}

func a2(){
    panic(errors.New("if a panic is done without an error interface the panic will be re-raised"))
}

func a3(){
    panic(errors.New("if a panic is done without an error interface the panic will be re-raised"))
}

# Functions

No description provided by the author
ExpectedPanicAsError sets the error pointer with the expected panic if panic is NOT an error interface it will be ra-raised note: this function must be deferred e.g.
GetCode returns the code for an error if it has one.
GetInternalErrorMessage returns the internal error message for an error if it has one.
GetStatusCode returns the status code for an error if it has one.
GetTags returns all the tags for a given error.
IsAccessDenied checks if an error exhibits AccessDenier behavior.
IsCodeable checks if an error exhibits Codeable behavior.
IsConflict checks if an error exhibits Conflicter behavior.
IsExist checks if an error exhibits Exister behavior.
IsInternalErrorMessage checks if an error exhibits InternalErrorMessagable behavior.
IsNotFound checks if an error exhibits NotFounder behavior.
IsRateLimit checks if an error exhibits RateLimiter behavior.
IsStackTraceable checks if an error exhibits taggable behavior.
IsStatusCodeable checks if an error exhibits StatusCodeable behavior.
IsTaggable checks if an error exhibits taggable behavior.
IsTemporary checks if an error exhibits Temporary behavior.
IsTooLarge checks if an error exhibits TooLarger behavior.
IsTooManyRequests checks if an error exhibits TooManyRequester behavior.
IsUnauthorized checks if an error exhibits Unauthorized behavior.
New creates a new error with the ability to add multiple behavior to that error e.g.
NewAccessDenied creates a new error and gives it AccessDenier behavior.
NewCode adds code to an error.
NewConflict creates a new error and gives it Conflicter behavior.
NewExists creates a new error and gives it Exister behavior.
NewInternalErrorMessage adds an internal message to an error.
NewNotFound creates a new error and gives it NotFounder behavior.
NewRateLimit creates a new error and gives it RateLimiter behavior.
No description provided by the author
NewStatusCode adds a status code to an error.
No description provided by the author
NewTagged adds tags to an error.
NewTemporary creates a new error and gives it Temporarily behavior.
NewTooLarge creates a new error and gives it TooLarge behavior.
NewTooManyRequests creates a new error and gives it TooManyRequester behavior.
NewUnauthorized creates a new error and gives it Unauthorizable behavior.
OnExpectedPanic recovers from an expected panic and provides expected error to the provided callback if panic is NOT an error interface it will be ra-raised example: defer OnExpected(func(err error){ doSomethingWithError(err) }) note: this should be used sparingly; public APIs should NOT expect consumers to handle panics.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Wrap wraps an error with the ability to add multiple behavior to that error e.g.
WrapAccessDenied wraps an error and gives it AccessDenier behavior.
WrapConflict wraps an error and gives it Conflicter behavior.
WrapExists wraps an error and gives it Exister behavior.
WrapNotFound wraps an error and gives it NotFounder behavior.
WrapRateLimit wraps an error and gives it RateLimiter behavior.
WrapTemporary wraps an error and gives it Temporarily behavior.
WrapTooLarge wraps an error and gives it TooLarge behavior.
WrapTooManyRequests wraps an error and gives it TooManyRequester behavior.
WrapUnauthorized wraps an error and gives it Unauthorizable behavior.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

AccessDenier determines if an error exhibits access denied behavior.
Codeable determines if an error exhibits code behavior.
Conflicter determines if an error exhibits conflict behavior.
Exister determines if an error exhibits exists behavior.
InternalErrorMessagable determines if an error has an internal error message.
NotFounder determines if an error exhibits not found behavior.
RateLimiter determines if an error exhibits rate limit behavior.
StackTraceable determines if an error exhibits stacktrace behavior from the pkg/errors package.
StatusCodeable determines if an error has a status code.
Taggable determines if an error exhibits tag behavior.
Temporarily determines if an error exhibits temporary behavior.
TooLarge determines if an error exhibits too large behavior.
TooManyRequester determines if an error exhibits too many requests.
Unauthorizable determines if an error exhibits unauthorized behavior.

# Type aliases

No description provided by the author
No description provided by the author
StringErr allows you to use a string as an error.