errors

package
v1.7.6 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

错误码

功能

  1. 需要能打印错误堆栈信息

  2. 能支持不同格式的打印,如%+v,%v,%s

    • %s 打印展示给用户的错误信息
    • %v alias for %s
    • %-v 打印堆栈,错误码,展示给用户的信息,展示给研发的信息(最外层)
    • %+v 打印堆栈,错误码,展示给用户的信息,展示给研发的信息(所有)
    • %#-v json 格式
    • %#+v json 格式
  3. 能支持wrap/unWarp 功能,在已有的错误信息上追加其他错误信息。

  4. 错误应有Is方法

  5. 错误包应支持As函数

  6. 支持两种创建方式,格式化和非格式化

    即 errors.New 和 errors.Errorf

  7. 能通过错误来确定接口返回 status code

Code 设计

设计原则

  1. 应当方便定位问题和代码行
  2. 错误码包含错误级别,错误模块,具体错误信息

Code 格式

XXYYZZ, XX 代表服务,YY 代表模块, ZZ 代表错误码序号

error 类型

  • withStack, 封装原有的错误, 并添加调用栈信息。支持 %v 格式化打印调用栈信息。 主要用于debug 信息。 不建议将此错误打印到除Debug level 以外的日志中。
  • withMessage, 包含withStack。 支持 %s 和 %v 格式化。 %s 将只打印错误信息。%v 将包含调用栈。
  • withCode, 包含 withMessage。 可以用%s 记录日志。 %v 打印调用栈, withCode 打印的调用栈相对上面的两个error 会相对简单,只显示一层, 可以打印到日志中去,方便分析。同时多层withCode 会递归打印每个withCode 的调用位置。withCode error 可以获得一个coder 来打印用户友好的错误信息。方便返回到api 中。

错误信息规范说明

  • 对外暴露的错误,统一大写开头,结尾不要加.。
  • 对外暴露的错误要简洁,并能准确说明问题。
  • 对外暴露的错误说明,应该是 该怎么做 而不是 哪里错了。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func New

func New(message string) error

func WithCode

func WithCode(err error, code int, message string) error

func WithCodef

func WithCodef(err error, code int, format string, args ...interface{}) error

func WithMessage

func WithMessage(err error, s string) error

Types

type CodeError added in v1.7.5

type CodeError interface {
	error
	Code() int
	Is(target error) bool
}

func ErrorWithCause added in v1.7.5

func ErrorWithCause(err CodeError, cause error) CodeError

func NewCodeError added in v1.7.5

func NewCodeError(code int, message string) CodeError

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   function name and path of source file relative to the compile time
      GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v   equivalent to %+s:%d

func (Frame) MarshalText

func (f Frame) MarshalText() ([]byte, error)

MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format formats the stack of Frames according to the fmt.Formatter interface.

%s	lists source files for each Frame in the stack
%v	lists the source file and line number for each Frame in the stack

Format accepts flags that alter the printing of some verbs, as follows:

%+v   Prints filename, function, and line number for each Frame in the stack.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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