errorx

package
v0.0.0-...-270bddf Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 2 Imported by: 0

README

errorx 包

errorx 包提供了一个增强的错误处理机制,特别适用于 HTTP 服务。它定义了自定义的错误类型,包含 HTTP 状态码、错误码和原因,并提供了创建、包装和处理这些错误的方法。

类型

HttpStatusCode

HttpStatusCode 是一个表示 HTTP 状态码的类型别名:

type HttpStatusCode int

预定义的 HTTP 状态码常量包括:

  • ErrBadRequest (400): 表示客户端发送的请求有错误
  • ErrUnauthorized (401): 表示请求需要认证
  • ErrForbidden (403): 表示服务器拒绝请求
  • ErrNotFound (404): 表示请求的资源不存在
  • ErrMethodNotAllowed (405): 表示请求方法不允许
  • ErrNotAcceptable (406): 表示请求的资源不支持请求的格式
  • ErrInternalServerError (500): 表示服务器内部错误
ErrorCode

ErrorCode 是一个表示错误码的类型别名:

type ErrorCode string

Error

Error 结构体表示一个增强的错误,包含 HTTP 状态码、错误码和原因:

type Error struct { HttpStatusCode HttpStatusCode ErrorCode ErrorCode Reason string }

ResponseErr

ResponseErr 结构体用于 JSON 响应中的错误信息:

type ResponseErr struct { Code string json:"code" Reason string json:"reason" }

方法

Error.Error()

实现了 error 接口,返回错误的原因。

Error.WithCause(cause string) error

为现有的 Error 添加额外的错误原因,返回一个新的错误。

WithCause(err error, cause string) error

为任意错误添加额外的错误原因。如果输入是 *Error 类型,它会调用相应的方法;否则,它会创建一个新的错误,将原始错误包装在其中。

Error.Data() (HttpStatusCode, *ResponseErr)

返回错误的 HTTP 状态码和用于响应的错误数据。

NewError(httpStatuCode HttpStatusCode, errorCode ErrorCode, reason string) *Error

创建并返回一个新的 Error 实例。

使用示例

// 创建一个新的错误
err := errorx.NewError(errorx.ErrBadRequest, "ERR001", "Invalid input")

// 添加额外的错误原因
newErr := errorx.WithCause(err, "Missing required field")

// 获取错误数据用于 HTTP 响应
statusCode, respErr := newErr.Data()

// 使用错误数据
fmt.Printf("Status Code: %d\n", statusCode)
fmt.Printf("Error Code: %s\n", respErr.Code)
fmt.Printf("Error Reason: %s\n", respErr.Reason)

这个包设计用于简化 HTTP 服务中的错误处理,提供了一种统一的方式来创建、包装和处理错误,同时保持与 HTTP 状态码的一致性。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCause

func WithCause(err error, cause string) error

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func NewError

func NewError(httpStatuCode HttpStatusCode, errorCode ErrorCode, reason string) *Error

NewError 创建一个新的错误

func (*Error) Data

func (e *Error) Data() *ResponseErr

Data 返回错误数据

func (*Error) Error

func (e *Error) Error() string

Error 实现 error 接口

func (*Error) ErrorCode

func (e *Error) ErrorCode() string

func (*Error) HttpStatusCode

func (e *Error) HttpStatusCode() int

func (*Error) Is

func (e *Error) Is(target error) bool

Is 判断错误是否相等

func (*Error) WithCause

func (e *Error) WithCause(cause string) error

WithCause 添加错误原因

type ErrorCode

type ErrorCode string

ErrorCode 表示错误码

type HttpStatusCode

type HttpStatusCode int

HttpStatusCode 表示 HTTP 状态码

const (
	// ErrBadRequest 400: 表示客户端发送的请求有错误
	ErrBadRequest HttpStatusCode = http.StatusBadRequest

	// ErrUnauthorized 401: 表示请求需要认证
	ErrUnauthorized HttpStatusCode = http.StatusUnauthorized

	// ErrForbidden 403: 表示服务器拒绝请求
	ErrForbidden HttpStatusCode = http.StatusForbidden

	// ErrNotFound 404: 表示请求的资源不存在
	ErrNotFound HttpStatusCode = http.StatusNotFound

	// ErrMethodNotAllowed 405: 表示请求方法不允许
	ErrMethodNotAllowed HttpStatusCode = http.StatusMethodNotAllowed

	// ErrNotAcceptable 406: 表示请求的资源不支持请求的格式
	ErrNotAcceptable HttpStatusCode = http.StatusNotAcceptable

	// ErrInternalServerError 500: 表示服务器内部错误
	ErrInternalServerError HttpStatusCode = http.StatusInternalServerError
)

type ResponseErr

type ResponseErr struct {
	Code   string `json:"code"`
	Reason string `json:"reason"`
}

ResponseErr 响应错误

Jump to

Keyboard shortcuts

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