Categorygithub.com/Darevski/go-custom-errors
modulepackage
1.2.1
Repository: https://github.com/darevski/go-custom-errors.git
Documentation: pkg.go.dev

# README

Go Custom Errors

Extended Errors for Go

What? Why?

It is extremely important to be able to effectively debug problems in production. However, depending on the simplicty of your language's error handling, managing errors and debugging can become tedious and difficult. For instance, GoLang provides a basic error which contains a simple value - but it lacks verbose information about the cause of the error or the state of the stack at error-time.

Custom Errors uses original GoLang built-in error types but stores additional extra data.

These are the next extra extended features that present in package:

  1. Wrapping Error - the original error which happened in the system or error that has been created by this package could be wrapped using this package.
  2. Wrapping Stack Generation - errors wrapping allow package to generate stack of errors
  3. Additional Information - every custom error has information about place of error appearing, error code, information of error data layer level, error severity
  4. Baggage Data - a map which could be used to store custom data associated with an error.
  5. Multiple Error Storage - package provides the ability to store multiple errors, with further operations with them, like search etc.

Quick Usage

Basics

import cErrors "github.com/Darevski/go-custom-errors"

//....

func MyFunc() error{
    // Create new Custom Error that implements standard golang Error interface
	err := cErrors.New(ErrorType, ErrorLevel, ErrorBaggage, ErrorSeverity, ErrorMessage)
	return err
    // .....
}

//....

if data, err := MyFunc(); err != nil {

    // Check Error of MyFunc function && if it`s Custom Error then check is it NotFound error 
	// If it is, then log that with custom message
    if custom, ok := err.(*cErrors.CustomError); ok && custom.IsErrorWithCodeExistInStack(cErrors.NotFound) {
    	logger.Log(custom.GetMessage())
    }
    //....
    
}

Custom data in error

func DoSomething(w http.ResponseWriter, r *http.Request) {
	
    //......
	
    if err = ReadJSONFromReader(r, someModel); err != nil {
        err = cErrors.Wrap(err, "Error on JSON read")
        
        err.AddBaggage(cErrors.ErrorBaggage{
            "JsonReaderID" : someModel.ID
        })
    }
    
    //......
}

Wrap error

func DoSomething(w http.ResponseWriter, r *http.Request) {
	
    //......
	
    if err = ReadJSONFromReader(r, someModel); err != nil {
        err := cErrors.WrapF(err, "Read error for %s", r)
        err.AddBaggage(cErrors.ErrorBaggage{
            "JsonReaderID" : someModel.ID
        })
    }
    
    //......
}

Available Interfaces

All methods of CustomError and MultipleCustomErrs can be viewed at this file

Error Codes && DataLayers|Severity Levels

The following error codes, dataLayer and severity levels are currently available:

const (
	DefaultLevel = ErrorLevel(iota)
	DataLevel
	UseCaseLevel
	ContainerLevel
	ControllerLevel
	TransportLevel
)

const (
	DefaultSeverity = ErrorSeverity(iota)
	Debug
	Info
	Warning
	Critical
	Fatal
	Panic
)

const (
	DefaultType = ErrorType(iota)
	NotFound
	InvalidArguments

	InternalError
	BadRequest

	AccessDenied
	Unauthorized
)

Fell free to use any INT code for own codes/levels

# Functions

No description provided by the author
New create custom error with the provided params && error message.
NewBase create custom error with specified message also all error attributes are set to default values.
NewBaseF create custom error with error message that formats according to a format specifier also all error attributes are set to default values.
NewF create custom error with params && error message that formats according to a format specifier.
NewMultiply create Multiple Errors representation struct that allowed to use MultipleCustomErrs interface.
Wrap error with message.
WrapF is analogous to the Wrap method, except that instead of ErrorMessage there are formatting arguments for the message.

# Constants

ErrorType describes the type of error.
ErrorType describes the type of error.
ErrorLevel describes the data level of error, based on "clean architecture".
ErrorLevel describes the data level of error, based on "clean architecture".
ErrorSeverity describes the severity of error.
ErrorLevel describes the data level of error, based on "clean architecture".
ErrorSeverity describes the severity of error.
ErrorLevel describes the data level of error, based on "clean architecture".
ErrorSeverity describes the severity of error.
ErrorType describes the type of error.
ErrorSeverity describes the severity of error.
ErrorSeverity describes the severity of error.
ErrorType describes the type of error.
ErrorType describes the type of error.
ErrorType describes the type of error.
ErrorSeverity describes the severity of error.
ErrorLevel describes the data level of error, based on "clean architecture".
ErrorType describes the type of error.
ErrorLevel describes the data level of error, based on "clean architecture".
ErrorSeverity describes the severity of error.

# Interfaces

CustomError for custom customErr type.
MultipleCustomErrs for errSlice of custom errs.
No description provided by the author

# Type aliases

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