errors

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2021 License: MIT Imports: 7 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Log

func Log(err error)

Log 打印错误,配合通过 Wrapper 函数返回的 error 使用。 若未通过 SetLoggerWriter 设置过 writer,则默认打印到 stderr。 输出的格式为:

2020/05/31 16:38:09 an error was caught at runtime:
	@ [errors.handler]: can not get user by id (utils/errors/wrapper_test.go:30)
	@ [errors.UserModel.GetUserByID]: query fail (utils/errors/wrapper_test.go:39)
	@ [origin]: can not connect to db

func SetLoggerWriter

func SetLoggerWriter(out io.Writer)

SetLoggerWriter 改变 Log 函数的输出位置。

func SetWrapperOpt

func SetWrapperOpt(opt WrapperOpt)

SetWrapperOpt 根据 opt 改变 Wrapper 函数的行为, 该函数需在调用 Wrapper 函数前执行。

func Wrapper

func Wrapper(err error, format string, a ...interface{}) error

Wrapper 返回一个包装 err 的 wrapper error,该 wrapper error 携带了调用者调用时的源码文件名、行号和产生错误的函数名称。 用 Wrapper 函数包裹错误、向上传递、再包裹、再传递 ... 以这种方式来获取错误冒泡过程中每一层的堆栈信息。

Types

type WrapperOpt

type WrapperOpt struct {
	// ProjectDir 需设置为编译时所在环境的项目路径,
	// 该路径会作为前缀抵消堆栈信息里源码文件路径里的该部分,
	// 如果不设置或者值和编译时项目所在的路径不符,
	// 则堆栈信息里的源码文件路径则会展示执行编译时文件所在的绝对路径。
	//
	// 若项目文件夹名为 app,在执行编译时项目的路径为 /path/to/app/,
	// 则需将该值设置为 "/path/to/"。
	//
	// 注意:若使用 Docker 执行多阶段构建,则需将该值为 Docker 容器内项目的绝对路径。
	ProjectDir string
	// ProjectModule 需设置为项目的 Module 名称,即 go.mod 文件第一行指定的项目名称,
	// 目的同样是为了去除 error 堆栈中函数名称前缀。
	//
	// 若项目 module 名称为 example.com/foo/bar,
	// 则需将该值设置为 "example.com/foo/bar/"。
	ProjectModule string
}

WrapperOpt 是 Wrapper 设置项。

Jump to

Keyboard shortcuts

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