apperr

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package apperr 定义统一业务错误:HTTP 状态 + 业务码 + 消息 + 原始错误链。 与 webiris.OK/Fail 配合,业务代码返回 apperr.Error,由中间件统一转响应。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is

func Is(err error, code Code) bool

Is 判断错误链中是否存在指定业务码的错误。

Types

type Code

type Code int

Code 是业务错误码。

const (
	CodeOK           Code = 0
	CodeBadRequest   Code = 400
	CodeUnauthorized Code = 401
	CodeForbidden    Code = 403
	CodeNotFound     Code = 404
	CodeInternal     Code = 500
)

常用业务错误码(与 HTTP 状态一致,便于调试;业务可自定义扩展码)。

type Error

type Error struct {
	HTTPStatus int
	Code       Code
	Message    string
	Cause      error
}

Error 是统一业务错误。 Message 会直接暴露给调用方,不得包含敏感信息;原始错误通过 Cause 保留在服务端日志。

func From

func From(err error) *Error

From 把任意 error 转换为 *Error: 已是 *Error 时原样返回;其他错误转换为 500 内部错误(服务端应记录 Cause)。

func New

func New(httpStatus int, code Code, message string) *Error

New 创建业务错误(无原始错误)。

func Newf

func Newf(httpStatus int, code Code, format string, args ...interface{}) *Error

Newf 创建带格式化消息的业务错误。

func Wrap

func Wrap(err error, httpStatus int, code Code, message string) *Error

Wrap 包装原始错误为业务错误;原始错误不出现在 Message 中。

func (*Error) Error

func (e *Error) Error() string

Error 实现 error 接口。

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 支持 errors.Is/As 定位原始错误。

Jump to

Keyboard shortcuts

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