errors

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2019 License: MIT Imports: 11 Imported by: 31

README

errors

A golang package to extend standard golang error mechanism, tracks who caused the error, helps error display and error report.

Documentation

Overview

Package errors Enhanced errors package, add CausedBy information. Can replace standard errors package.

Error classified by who caused the error:

  1. Bug: we programmers are the one responsible for the error.
  2. Runtime: OS, hardware, golang, code libraries, normally can be fixed by DevOp.
  3. External: caused by external service, or network connection.
  4. Input: caused by bad input.

The first two kinds can be fixed by ourself, and the last two we can not fixed must report back.

Index

Constants

View Source
const (
	// NotError returned by GetCode() if value is nil.
	NotError = Code(0)

	// GeneralByBug generic ByBug error code
	GeneralByBug = Code(ByBug)

	// GeneralByRuntime general ByRuntime error code
	GeneralByRuntime = Code(ByRuntime)

	// GeneralByExternal general ByExternal error code
	GeneralByExternal = Code(ByExternal)

	// GeneralByInput general ByInput error code
	GeneralByInput = Code(ByInput)

	// GeneralByClientBug general ByClientBug error code
	GeneralByClientBug = Code(ByClientBug)
)

Variables

This section is empty.

Functions

func Close

func Close(c io.Closer)

Close close an IO closer, handle by errors.Handle() on failed.

It is very often to forget to close a closer, such as file, net connection, and very annoying to handle this kind normally won't failed error.

func ForLog

func ForLog(v interface{}) string

ForLog convert value to string for better logging:

  1. if v is *Error, use .ErrorStack
  2. if v is error, use .Error()
  3. otherwise, use fmt.Sprint(v)

func Handle

func Handle(ctx context.Context, err interface{})

Handle use handler to handle non-nil err value. Use SetHandler() to switch handler, default handler is a plain log.Print(), if ctx is nil, pass context.Background() to error handler.

func SetHandler

func SetHandler(h Handler)

SetHandler switch error handler, NOTE: no sync lock to internal handler variable, only call SetHandler in application initialization code, to prevent data race. If h is nil, reset to default handler, this feature only available in test mode for unit tests to override error handler.

Types

type CausedBy

type CausedBy uint32

CausedBy describe who caused this error.

const (
	// ByBug error caused by a bug. This kind of error normally logged and/or
	// report to error report service, notify to developer. Did not show detail
	// error to end-user, it is not their fault, do not blame them, and apologize
	// for this internal error.
	ByBug CausedBy = (iota + 1) << 24

	// ByRuntime error caused by golang runtime, such as run out of memory, file
	// read/write error. Any error caused by OS, or hardware. Network interface
	// error is caused by Runtime, error caused by network cable is not. Report
	// this kind of error to health monitor service, notify maintains team as
	// fast as possible. It not need to report to error service.
	ByRuntime

	// ByExternal error caused by depended external service, such as a Database
	// or other app services. Or network environment, such as lost network
	// connection. This kind of error is can not fixed by patching code, patching
	// OS, upgrade or replace hardware, they are not our error, nothing we can
	// do.  Report to health monitor service, notify maintains team to contact
	// people who can fix this.
	// Give end-user a brief message that who caused this error that they can
	// understand, such as payment service, and user know this error is not
	// caused by us.
	ByExternal

	// ByInput error caused by bad input. A program always dealing with input,
	// such as user input, or a request for a service daemon. When the input is
	// not expected, return error with detail and precise reason. Of course, do
	// not need report to error report service or health monitor service.
	ByInput

	// ByClientBug error caused by wrong implemented client software, such as
	// wrong argument. ByInput is error caused by user. Normally ByClientBug not
	// report to health/crash report service.
	ByClientBug

	// NoError is a special value returned by GetPanicCausedBy() to indicate no
	// error happened
	NoError
)

func GetCausedBy

func GetCausedBy(e error) CausedBy

GetCausedBy from any error. If the error is Error interface, call its CausedBy() method. Then all considered as ByBug.

If the error is not a bug, wrap it use NewXXX() function before return:

 if err := os.Open("file"); err != nil {
		return errors.NewRuntime(err)
 }

func GetPanicCausedBy

func GetPanicCausedBy(v interface{}) CausedBy

GetPanicCausedBy resolve caused for recover() return value, if the value is nil, return NoError. For error value use GetCausedBy() to resolve, other value return ByBug.

func (CausedBy) String

func (i CausedBy) String() string

type CausedByError

type CausedByError interface {
	error

	// Inner error maybe nil
	Inner() error

	Code() Code

	// ErrorStack returns a string that contains both the
	// error message and the callstack.
	ErrorStack() string
}

CausedByError is interface provided CausedBy info. CausedByError make custom Error implementation possible.

type Code

type Code uint32

Code each code is an error, first 8 bit is CausedBy.

func GetCode

func GetCode(v interface{}) Code

GetCode returns error code from any value, if v is not CausedByError, returns GenericByBug. Returns NotError if v is nil.

func NewCode

func NewCode(cause CausedBy, low uint32) Code

NewCode create a new code

func (Code) Caused

func (code Code) Caused() CausedBy

Caused corresponding CausedBy value

type Error

type Error struct {
	Err error
	// contains filtered or unexported fields
}

Error contains error, causedBy, and stack.

func Bug

func Bug(text string) *Error

Bug creates an Error from string.

func Bugf

func Bugf(text string, a ...interface{}) *Error

Bugf sprintf version of Bug().

func Caused

func Caused(causedBy CausedBy, text string) *Error

Caused create error causedBy set by argument

func Causedf

func Causedf(causedBy CausedBy, text string, a ...interface{}) *Error

Causedf sprintf version of Caused

func ClientBug

func ClientBug(text string) *Error

ClientBug creates an Error from string.

func ClientBugf

func ClientBugf(text string, a ...interface{}) *Error

ClientBugf sprintf version of ClientBug.

func External

func External(text string) *Error

External creates an Error from string.

func Externalf

func Externalf(text string, a ...interface{}) *Error

Externalf sprintf version of Runtime().

func Input

func Input(text string) *Error

Input creates an Error from string.

func Inputf

func Inputf(text string, a ...interface{}) *Error

Inputf sprintf version of Input.

func New

func New(text string) *Error

New function replace of standard errors.New(), create a ByBug error.

func NewBug

func NewBug(e error) *Error

NewBug wrap an exist error to ByBug. If e is nil, return nil. If e is already an Error, wrap it to ByBug.

func NewCaused

func NewCaused(causedBy CausedBy, err error) *Error

NewCaused wraps an exist error to specified causedBy Error, If e is nil, return nil. If already an Error, returned directly if causedBy matches, re-wrap with specific causedBy if not matched.

func NewClientBug

func NewClientBug(e error) *Error

NewClientBug wrap an exist error to ByRuntime. If e is nil, return nil. If e is already an Error, wrap it to ByClientBug.

func NewExternal

func NewExternal(e error) *Error

NewExternal wrap an exist error to ByRuntime. If e is nil, return nil. If e is already an Error, wrap it to ByExternal.

func NewInput

func NewInput(e error) *Error

NewInput wrap an exist error to ByRuntime. If e is nil, return nil. If e is already an Error, wrap it to ByInput.

func NewRuntime

func NewRuntime(e error) *Error

NewRuntime wrap an exist error to ByRuntime. If e is nil, return nil. If e is already an Error, wrap it to ByRuntime.

func Runtime

func Runtime(text string) *Error

Runtime creates an Error from string.

func Runtimef

func Runtimef(text string, a ...interface{}) *Error

Runtimef sprintf version of Runtime().

func Wrap

func Wrap(causedBy CausedBy, err interface{}, text string) *Error

Wrap an exist error

func Wrapf

func Wrapf(causedBy CausedBy, err interface{}, text string, a ...interface{}) *Error

Wrapf is format version of Wrap()

func (*Error) Code

func (err *Error) Code() Code

Code returns error code.

func (*Error) Error

func (err *Error) Error() string

func (*Error) ErrorStack

func (err *Error) ErrorStack() string

ErrorStack returns a string that contains both the error message and the callstack, and inner Error's ErrorStack().

func (*Error) Inner

func (err *Error) Inner() error

Inner returns inner error (.Err field), implements CausedByError interface

func (*Error) Stack

func (err *Error) Stack() string

Stack returns the callstack formatted the same way that go does in runtime/debug.Stack()

func (*Error) StackFrames

func (err *Error) StackFrames() []StackFrame

StackFrames returns an array of frames containing information about the stack.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap is alias of Inner method, work with errors.Unwrap() of go 1.13

type Handler

type Handler func(ctx context.Context, err interface{})

Handler is a function do the actual error handling.

type StackFrame

type StackFrame struct {
	// The path to the file containing this ProgramCounter
	File string
	// The LineNumber in that file
	LineNumber int
	// The Name of the function that contains this ProgramCounter
	Name string
	// The Package that contains this function
	Package string
	// The underlying ProgramCounter
	ProgramCounter uintptr
}

A StackFrame contains all necessary information about to generate a line in a callstack.

func NewStackFrame

func NewStackFrame(pc uintptr) (frame StackFrame)

NewStackFrame popoulates a stack frame object from the program counter.

func (*StackFrame) Func

func (frame *StackFrame) Func() *runtime.Func

Func returns the function that contained this frame.

func (*StackFrame) SourceLine

func (frame *StackFrame) SourceLine() (string, error)

SourceLine gets the line of code (from File and Line) of the original source if possible.

func (*StackFrame) String

func (frame *StackFrame) String() string

String returns the stackframe formatted in the same way as go does in runtime/debug.Stack()

Directories

Path Synopsis
Contains common structure handles errors in CausedBy way in command line utility applications.
Contains common structure handles errors in CausedBy way in command line utility applications.

Jump to

Keyboard shortcuts

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