errors

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 5 Imported by: 82

README

README

An errors recorder package

This design supplements and implements the errors interface of golang, Used to provide information such as the cause and file location of the error when it occurs, in order to restore the happened scene.

Due to the large amount of data contained in this erroneous design, it is necessary to pay attention to the frequency of calls to avoid affecting system efficiency.

  • Example
package main

import "github.com/gwaylib/errors"

func fn1(a int) error {
    if a == 0 {
        // return a common no data error
        return errors.ErrNoData.As(a)
    }
    // other errors with fault position
    return errors.New("not implements").As(a)
}

func fn2(b int) error {
    // call and set the error position, do nothing if 'fn1' return nil
    return errors.As(fn1(b))
}

func main() {
    err := fn2(2)
    if err != nil {
        // Attention, errors.ErrNoData == err may not necessarily hold, the Equal method should be used
        if !errors.ErrNoData.Equal(err) {
            panic(err)
        }
        fmt.Println(err)
    }
}
  • Analyze errors position information
Output:
["test",["errors_test.go:90#errors.TestAs"],["errors_test.go:95#errors.TestAs",123,456]]

Decode: 
ErrData[0] -- the input of errors.New()
ErrData[1:] -- position information
ErrData[1][0] -- the position information when first calling 'As'
ErrData[1][1:] -- the args when first calling 'As'

Error handling suggestions

*) Prioritize handling errors before handling normal logic, as errors are less likely to be ignored and make the program more robust; *) Unless the error handling result is clearly defined, errors should always be returned to the caller; *) If it is not possible to return to the caller, user prompts or logs should be provided instead of discarding errors to fully understand what has happened in the program; *) Normal logic should not be written in if conditions to ensure good text indentation and reading of the code;

// Suggest
rows, err := db.Query(...)
if err != nil{
    return errors.As(err)
}
defer rows.Close()
...

// Unsuggest
rows, err := db.Query(...)
if err == nil{
    defer rows.Close()
    // ...
} else {
    // handle error or not
}

*) Define errors within a small scope, otherwise there is a risk of diffusion

func Get() (err error){
    // Suggest
    rows, err := db.Query(...)
    if err != nil{
        return errors.As(err)
    }
    defer rows.Close()
  
    if err := rows.Scan(...); err != nil{
        // ...
    }
    ...
}

Documentation

Overview

错误记录器 error recorder

本设计补充并实现了系统的error接口。 This design complements and implements the error interface of the go package.

用于发生错误时附带发生错误的原因、位置等信息以便还原现场。 It can be used to restore the scene with the information of the cause and location of the error when it occurs.

因本错误设计含有比较大的数据量信息,因此需要注意被调用的频率,以避免影响到系统效率。 Because this incorrect design contains a large amount of data information, we need to pay attention to the frequency of calls to avoid affecting system efficiency.

使用例子 Example

package main

import "github.com/gwaylib/errors"

func fn1(a int) error {
   if a == 1{
       return errors.ErrNoData.As(a)
   }
   return errors.New("not implements").As(a)
}

func fn2(b int) error {
   return errors.As(fn1(b))
}

func main() {
   err := fn2(2)
   if err != nil {
       // errors.ErrNoData == err not necessarily true, so use Equal instead.
       if !errors.ErrNoData.Equal(err) {
           panic(err)
       }

       // Deals the same error.
       fmt.Println(err)
   }
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoData = New("data not found")
)

Functions

func Equal

func Equal(err1 error, err2 error) bool

Compare two error is same instance or code is match.

func Is

func Is(err1 error, err2 error) bool

Compatible with official errors.Is

Types

type ErrData

type ErrData []interface{} // [0] is code, [1][0] is where, [1][1] is reason args

type Error

type Error interface {
	// Return the code of make.
	Code() string

	// Implement the error interface of go package
	Error() string
	// Impelment the json marshal interface of go package.
	MarshalJSON() ([]byte, error)

	// Record the stack when call, and return a new error with new stack.
	As(arg ...interface{}) Error

	// Compare to another error
	// It should be established with err1.Code() == err2.Code().
	Equal(err error) bool
}

func As

func As(err error, reason ...interface{}) Error

Record the reason with as, and return a new error with new stack of reason. It would be safe for concurrency.

func New

func New(code string) Error

Make a new error with Error type.

func Parse

func Parse(src string) Error

Parse from a Error serial.

func ParseError

func ParseError(src error) Error

Parse Error from a error instance. If the error is type of Error, it will be return directly.

func Wrap

func Wrap(err error, arg ...interface{}) Error

Same as 'As', just implement the errors system package

Jump to

Keyboard shortcuts

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