fserr

package module
v0.0.0-...-7ebe45c Latest Latest
Warning

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

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

README

goerr

Golang错误包,无缝切换原生errors包至goerr。

安装

go get github.com/yushengji/goerr

特性

  • 支持堆栈打印
  • 新建一个字符串错误
  • 包装已有错误
  • 业务错误码支持
  • 覆盖常用错误包的工具函数支持

新建错误

使用New得到一个错误,使用%+v占位符格式化打印即可看到堆栈信息,如果只想看到错误信息也可以使用%s或.Error

package main

import (
    "fmt"
    "github.com/yushengji/goerr"
)

func main() {
    err := goerr.New("this is a error")
    fmt.Sprintf("%+v", err)
}

包装错误

使用Wrap包装已有错误,并添加额外信息,例如下层业务报错信息为:db error,使用Wrap可以给用户提供更加人性化的提示信息,例如:系统繁忙,请重试

package main

import (
    "fmt"
    "github.com/yushengji/goerr"
)

func main() {
    err := goerr.Wrap(goerr.New("db error"), "系统繁忙,请重试")
    fmt.Sprintf("%+v", err)
}

业务错误码

在这里使用NewXX绑定了ErrBasic错误码(它是一个数字)与HTTP错误码,使用WithCode可以将错误码绑定到错误上。 对于业务错误码还可以使用goerr.ParseCode获取业务错误码、HTTP状态码,错误信息将使用错误码附带的错误信息,也就是示例中的basic error, 如果想要修改错误信息,则可以直接在WithCode后添加WithMessage参数即可覆盖。

package main

import (
    "fmt"
    "github.com/yushengji/goerr"
)

func main() {
	goerr.NewInternalError(goerr.ErrBasic, "basic error")
    err := goerr.WithCode(goerr.New("inner error"), goerr.ErrBasic)
    fmt.Sprintf("%+v", err)
	codeErr := goerr.ParseCode(err)
	fmt.Sprintf("http code is %d", codeErr.HttpCode)
}

性能

11th i7 16G Golang 1.22版本下,新建错误堆栈层数为10层性能如下:

操作 New Wrap WithCode
时间 801 ns/op 826.2 ns/op 2077 ns/op

Documentation

Index

Constants

View Source
const (
	ErrBasic = iota + 1
	ErrDb
	ErrParam
	ErrRetry
	ErrServiceInvoke
)

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As 匹配最外层的与target类型相同的error,将其赋值给target

func Is

func Is(err, target error) bool

Is 判断err是否是target类型 相比==判断错误,该方式会进行不断地类似于 UmWrap 的操作, 将 UmWrap 后的错误进行比较

func IsCode

func IsCode[T codeType](err error, code T) bool

IsCode 判断某个错误是否为某个错误码

func New

func New(format string, args ...any) error

New 创建新的错误,支持格式化占位符

func ParseCode

func ParseCode(err error) *withCode

ParseCode 将错误解析为错误码错误 若err不是错误码错误,则包裹传递错误,其他信息为默认错误码信息 若err为错误码错误,将其转换,将最外层错误信息作为最终错误信息返回 若想得到原始的错误码错误,可以使用As方法

func SetAppCode

func SetAppCode[T codeType](code T)

SetAppCode 设置服务错误码 模块码、模块错误码一共四位,指定应用码将拼接在前 例如应用码位101,模块码为1,模块错误码为21,那么最终业务错误码为:1010121

func SetDefault

func SetDefault(httpCode, businessCode int, message string)

SetDefault 设置默认错误码 当错误码匹配失败时,提供的备选方案,已内置默认错误码, 它的HTTP码为200,业务码和信息均为零值

func TransferFromError

func TransferFromError(err error) error

TransferFromError 将错误转为带栈帧的错误 Deprecated: 请使用 New、 Wrap、 WithCode、 WithStack

func UnWrap

func UnWrap(err error) error

UnWrap 获取包装过的error 若error1使用 Wrap 包装后产出错误error2 当使用 UmWrap 后,返回的是error1

func WithCode

func WithCode[T codeType](err error, businessCode T, options ...Option) error

WithCode 创建带有错误码的error,支持格式化占位符 使用option可以替换其中信息

func WithStack

func WithStack(err error) error

func Wrap

func Wrap(err error, format string, args ...any) error

Wrap 包装已有错误,支持格式化占位符

Types

type ErrCode

type ErrCode struct {
	// HttpCode 该错误码建议的HTTP响应码
	HttpCode int `json:"httpCode,omitempty"`
	// BusinessCode 该错误码对应的业务码
	// +optional
	BusinessCode int `json:"businessCode,omitempty"`
	// Message 该错误码对应的错误信息
	// +optional
	Message string `json:"message,omitempty"`
}

ErrCode 产生error所包含的错误码信息 支持http错误码、业务错误码 可通过下方快捷函数快速创建指定http码的错误码

func NewAlreadyExists

func NewAlreadyExists(businessCode int, message string) ErrCode

func NewBadRequest

func NewBadRequest(businessCode int, message string) ErrCode

func NewCode

func NewCode(httpCode, businessCode int, message string) ErrCode

NewCode 创建指定信息的错误码

func NewConflict

func NewConflict(businessCode int, message string) ErrCode

func NewForbidden

func NewForbidden(businessCode int, message string) ErrCode

func NewGenerateNameConflict

func NewGenerateNameConflict(businessCode int, message string) ErrCode

func NewGone

func NewGone(businessCode int, message string) ErrCode

func NewInternalError

func NewInternalError(businessCode int, message string) ErrCode

func NewMethodNotSupported

func NewMethodNotSupported(businessCode int, message string) ErrCode

func NewNotFound

func NewNotFound(businessCode int, message string) ErrCode

func NewOK

func NewOK(businessCode int, message string) ErrCode

func NewRequestEntityTooLargeError

func NewRequestEntityTooLargeError(businessCode int, message string) ErrCode

func NewServiceUnavailable

func NewServiceUnavailable(businessCode int, message string) ErrCode

func NewTimeoutError

func NewTimeoutError(businessCode int, message string) ErrCode

func NewTooManyRequests

func NewTooManyRequests(businessCode int, message string) ErrCode

func NewTooManyRequestsError

func NewTooManyRequestsError(businessCode int, message string) ErrCode

func NewUnauthorized

func NewUnauthorized(businessCode int, message string) ErrCode

type Option

type Option func(*withCode)

func WithMessage

func WithMessage(msg string) Option

WithMessage 替换错误码默认的提示信息

Jump to

Keyboard shortcuts

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