errors

package
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 8 Imported by: 0

README

Error package

This package allows extending go error interface to add parent tree, code of error, trace, ... This package still compatible with error interface and can be customized to define what information is return for standard go error implementation.

The main mind of this package is to be simple use, quickly implement and having more capabilities than go error interface

Example of implement

You will find many uses of this package in the golib repos.

We will create an error.go file into any package, and we will construct it like this :

import errors "github.com/nabbar/golib/errors"

const (
    // create const for code as type CodeError to use features func
    // we init the iota number to an minimal free code
    // golib/errors expose the minimal available iota with const : errors.MIN_AVAILABLE
    // with this method, all code uint will be predictable
	EMPTY_PARAMS errors.CodeError = iota + errors.MIN_AVAILABLE
    MY_ERROR
    // add here all const you will use as code to identify error with code & message
)

func init() {
    // register your function getMessage
	errors.RegisterFctMessage(getMessage)
}

// This function will return the message of one error code
func getMessage(code errors.CodeError) (message string) {
	switch code {
	case EMPTY_PARAMS:
		return "given parameters is empty"
	case My_ERROR:
		return "example of message for code MY_ERROR"
	}

    // CAREFUL : the default return if code is not found must be en empty string !
	return ""
}

In go source file, we can implement the call to package golib/errors like this :

// will test if err is not nil
// if is nil, will return nil, otherwise will return a Error interface from the MY_ERROR code with a parent err
_ = MY_ERROR.Iferror(err)
// return an Error interface from the MY_ERROR code and if err is not nil, add a parent err
_ = MY_ERROR.ErrorParent(err)
// error can be cascaded add like this
_ = MY_ERROR.IfError(EMPTY_PARAMS.IfError(err))
return MY_ERROR.Error(EMPTY_PARAMS.ErrorParent(err))

Documentation

Index

Constants

View Source
const (
	MIN_PKG_Archive    = 100
	MIN_PKG_Artifact   = 200
	MIN_PKG_Certif     = 300
	MIN_PKG_Console    = 400
	MIN_PKG_Crypt      = 500
	MIN_PKG_Httpcli    = 600
	MIN_PKG_Httpserver = 700
	MIN_PKG_IOUtils    = 800
	MIN_PKG_LDAP       = 900
	MIN_PKG_Network    = 1000
	MIN_PKG_OAuth      = 1100
	MIN_PKG_Aws        = 1200
	MIN_PKG_Router     = 1300
	MIN_PKG_Semaphore  = 1400
	MIN_PKG_SMTP       = 1500
	MIN_PKG_Static     = 1600
	//	MIN_PKG_Status    = 1700 // unused
	//	MIN_PKG_Update    = 1800 // unused
	MIN_PKG_Version = 1900

	MIN_AVAILABLE = 2000
)
View Source
const UNK_MESSAGE = "unknown error"

Variables

This section is empty.

Functions

func ExistInMapMessage

func ExistInMapMessage(code CodeError) bool

func GetDefaultGlue

func GetDefaultGlue() string

GetDefaultGlue return the current glue used to joins errors with parents.

func GetDefaultPattern

func GetDefaultPattern() string

GetDefaultPattern return the current pattern used for string of error with code. The pattern is fmt pattern with 2 inputs in order : code, message.

func GetDefaultPatternTrace

func GetDefaultPatternTrace() string

GetDefaultPatternTrace return the current pattern used for string of error with code and trace. The pattern is fmt pattern with 3 inputs in order : code, message, trace.

func RegisterIdFctMessage

func RegisterIdFctMessage(minCode CodeError, fct Message)

func SetDefaultGlue

func SetDefaultGlue(glue string)

SetTracePathFilter define the glue string to be used to join main error with parents'errors.

func SetDefaultPattern

func SetDefaultPattern(pattern string)

GetDefaultPatternTrace define the pattern to be used for string of error with code. The pattern is fmt pattern with 2 inputs in order : code, message.

func SetDefaultPatternTrace

func SetDefaultPatternTrace(patternTrace string)

SetDefaultPatternTrace define the pattern to be used for string of error with code and trace. The pattern is fmt pattern with 3 inputs in order : code, message, trace.

func SetModeReturnError

func SetModeReturnError(mode ErrorMode)

func SetTracePathFilter added in v1.3.0

func SetTracePathFilter(path string)

SetTracePathFilter customize the filter apply to filepath on trace.

Types

type CodeError

type CodeError uint16
const UNK_ERROR CodeError = 0

func (CodeError) Error

func (c CodeError) Error(p Error) Error

func (CodeError) ErrorParent

func (c CodeError) ErrorParent(p ...error) Error

func (CodeError) GetInt

func (c CodeError) GetInt() int

func (CodeError) GetMessage

func (c CodeError) GetMessage() string

func (CodeError) GetString

func (c CodeError) GetString() string

func (CodeError) GetUint16

func (c CodeError) GetUint16() uint16

func (CodeError) IfError

func (c CodeError) IfError(e Error) Error

func (CodeError) Iferror

func (c CodeError) Iferror(e error) Error

type Error

type Error interface {
	IsCodeError(code CodeError) bool
	HasCodeError(code CodeError) bool

	IsError(e error) bool
	HasError(err error) bool
	HasParent() bool

	AddParent(parent ...error)
	SetParent(parent ...error)
	AddParentError(parent ...Error)
	SetParentError(parent ...Error)

	Code() string
	CodeFull(glue string) string
	CodeSlice() []string

	CodeError(pattern string) string
	CodeErrorFull(pattern, glue string) string
	CodeErrorSlice(pattern string) []string

	CodeErrorTrace(pattern string) string
	CodeErrorTraceFull(pattern, glue string) string
	CodeErrorTraceSlice(pattern string) []string

	Error() string

	StringError() string
	StringErrorFull(glue string) string
	StringErrorSlice() []string

	GetError() error
	GetErrorFull(glue string) error
	GetErrorSlice() []error

	GetIError() Error
	GetIErrorSlice() []Error

	GetTrace() string
	GetTraceSlice() []string
}

func MakeErrorIfError

func MakeErrorIfError(err ...Error) Error

func NewError

func NewError(code uint16, message string, parent Error) Error

func NewErrorIfError

func NewErrorIfError(code uint16, message string, parent Error) Error

func NewErrorIferror

func NewErrorIferror(code uint16, message string, parent error) Error

type ErrorMode

type ErrorMode uint8
const (
	Default ErrorMode = iota
	ErrorReturnCode
	ErrorReturnCodeFull
	ErrorReturnCodeError
	ErrorReturnCodeErrorFull
	ErrorReturnCodeErrorTrace
	ErrorReturnCodeErrorTraceFull
	ErrorReturnStringError
	ErrorReturnStringErrorFull
)

func GetModeReturnError

func GetModeReturnError() ErrorMode

func (ErrorMode) String

func (m ErrorMode) String() string

type Message

type Message func(code CodeError) (message string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL