errorsx

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

errorsx

errorsxerrors 包与 httpserver typed handler 之间的桥接层。

它的职责很单一:

  • errors.Code(err)errors.Name(err)errors.HTTPCode(err) 映射为稳定 HTTP 响应
  • 提供可直接挂到 httpserver.WithErrorMapper(...) 的适配器

安装

go get github.com/tsopia/go-kit/httpserver/integration/errorsx

使用

srv.POST("/users", httpserver.HandleJSON(
    createUser,
    httpserver.WithErrorMapper(errorsx.Mapper()),
))

默认响应示例:

{
  "code": 2002,
  "name": "INVALID_PARAM",
  "message": "email is required"
}

普通 error 会回退到内部错误响应,不直接暴露底层错误消息。

Documentation

Overview

Package errorsx 提供 errors 包与 httpserver typed handler 之间的桥接映射。

适用场景:

  • 团队统一使用 github.com/tsopia/go-kit/errors 作为业务错误出口
  • 需要把错误码、错误名和 HTTP 状态稳定映射到 typed handler 响应
  • 不希望 httpserver core 直接依赖 errors 包

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mapper

func Mapper() httpserver.ErrorMapper

Mapper 返回可直接挂到 typed handler 的 errors 到 HTTP 响应映射器。

Types

type ResponseBody

type ResponseBody struct {
	Code    int            `json:"code"`
	Name    string         `json:"name"`
	Message string         `json:"message"`
	Details map[string]any `json:"details,omitempty"`
}

ResponseBody 描述 errors 包桥接到 HTTP 响应时使用的默认结构。

func Response

func Response(err error) (int, ResponseBody)

Response 将 errors 包的业务错误映射为 HTTP 状态码和响应体。

Jump to

Keyboard shortcuts

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