package
0.0.0-20211113074651-c6ea6ab4ee08
Repository: https://github.com/tsaikd/kdgolib.git
Documentation: pkg.go.dev

# README

errutil

An error handling helper, providing more APIs than built-in package (errors, fmt), and compatible with go error interface

Why use errutil instead of built-in errors and fmt

Cover

Usage

  • Import package from master branch
import "github.com/tsaikd/KDGoLib/errutil"
  • Declare error factory
var ErrorOccurWithReason = errutil.NewFactory("An error occur, reason: %q")
  • Return error with factory
func doSomething() (err error) {
	// do something

	// return error with factory,
	// first argument is parent error,
	// the others are used for factory
	return ErrorOccurWithReason.New(nil, "some reason here")
}
  • Handle errors
func errorHandlingForOneCase() {
	if err := doSomething(); err != nil {
		if ErrorOccurWithReason.In(err) {
			// handling expected error
			return
		}

		// handling unexpected error
		return
	}
}
func errorHandlingForMultipleCases() {
	if err := doSomething(); err != nil {
		switch errutil.FactoryOf(err) {
		case ErrorOccurWithReason:
			// handling expected error
			return
		default:
			// handling unexpected error
			return
		}
	}
}

Optional usage

  • Import from v1 branch
import "gopkg.in/tsaikd/KDGoLib.v1/errutil"
  • Use like built-in errors package
    • bad case because all errors should be exported for catching by other package
func doSomething() (err error) {
	// do something

	// return error with factory
	return errutil.New("An error occur")
}

# Functions

AddParent add parent to errobj.
AddRuntimeCallerFilter add filters to DefaultRuntimeCallerFilter for RuntimeCaller().
AllNamedFactories return all named factories.
AllSortedNamedFactories return all sorted named factories NOTE: this is slow for sorting in runtime.
ContainErrorFunc check error contain error by custom equalFunc.
FactoryOf return factory of error, return nil if not factory found.
Length count number of ErrorObject and all parents, return -1 if error.
Logger return default LoggerType instance.
MarshalJSON marshal error to json.
New return a new ErrorObject object.
NewConsoleFormatter create JSONErrorFormatter instance.
NewErrors return ErrorObject that contains all input errors.
NewErrorsSkip return ErrorObject, skip function call.
NewFactory return new ErrorFactory instance.
NewJSON create ErrorJSON.
NewJSONFormatter create JSONFormatter instance.
NewLogger create LoggerType instance.
NewNamedFactory return new ErrorFactory instance with factory name, panic if name duplicated.
RuntimeCaller wrap runtimecaller.GetByFilters() with DefaultRuntimeCallerFilter.
RuntimeCallerFilterStopErrutilPackage filter CallInfo to stop after reach KDGoLib/errutil package.
RuntimeCallerFilterStopSQLUtilPackage filter CallInfo to stop after reach KDGoLib/sqlutil package.
SetDefaultLogger set default LoggerType.
Trace error stack, output to default ErrorFormatter, panic if output error.
TraceSkip error stack, output to default ErrorFormatter, skip n function calls, panic if output error.
TraceWrap trace errin and wrap with wraperr only if errin != nil.
WalkErrors walk from base error through all parents return ErrorWalkLoop if detected loop.
WriteCallInfo write readable callinfo with options.

# Variables

DefaultRuntimeCallerFilter use for filter error stack info.
errors.

# Structs

ConsoleFormatter used to format error object in console readable.
ErrorJSON is a helper struct for display error.
JSONFormatter used to format error to json.
LoggerOptions for Logger.

# Interfaces

ErrorFactory is used for create or check ErrorObject.
ErrorFormatter to format error.
ErrorObject is a rich error interface.
LoggerType declare general log types.
TraceFormatter to trace error occur line formatter.

# Type aliases

WalkFunc is a callback for WalkErrors.