Documentation
¶
Overview ¶
Package hah 提供默认的根包 HTTP 边界入口,聚合请求输入、公共错误模型与 JSON 响应写回。
适合在大多数 handler 中直接使用:
- 核心 request helper:Path、Query
- 明确分离的 DTO 绑定入口:BindQuery、BindBody
- 常见请求字段错误与公共错误模型:InvalidRequest、FieldError、HTTPError
- 常用 JSON 成功响应辅助
- 统一错误响应写回
当前项目里,hah 是默认入口;多数调用方不需要直接 import reqx。 只有当你明确在拆分 request-side 组件、并需要更低层的输入侧公开面时, 才退到 reqx.xx。
公开 API:
- request helper:Path、Query
- 绑定入口:BindQuery、BindBody
- 请求级规则 helper:InvalidRequest
- 公共错误模型:FieldError、HTTPError、NewHTTPError、NewHTTPErrorWithCause
- 常用错误快捷构造:BadRequest、Unauthorized、Forbidden、NotFound、 MethodNotAllowed、Conflict、UnprocessableEntity、TooManyRequests、 InternalServer
- 错误归一化入口:NormalizeError
- 公开 field error 常量:Code*、In*
- 错误响应入口:WriteError
- 成功响应入口:JSON、OK、Accepted、Created、NoContent
当前根包是默认且唯一推荐的公开入口;错误与响应边界固定收敛在这里。 reqx 仍然是公开包,但定位为较低层的 request-side 原生面,而不是并列主入口。
Index ¶
- Constants
- func Accepted(w http.ResponseWriter, data any) error
- func BindBody(r *http.Request, target any) error
- func BindQuery(r *http.Request, target any) error
- func Created(w http.ResponseWriter, data any) error
- func InvalidRequest(fieldErrors ...FieldError) error
- func JSON(w http.ResponseWriter, status int, data any) error
- func NoContent(w http.ResponseWriter) error
- func OK(w http.ResponseWriter, data any) error
- func Path(r *http.Request, name string) *reqx.PathParam
- func Query(r *http.Request, name string) *reqx.QueryParam
- func WriteError(w http.ResponseWriter, err error, topCode ...int) error
- type FieldError
- type HTTPError
- func BadRequest(code, detail string) *HTTPError
- func Conflict(code, detail string) *HTTPError
- func Forbidden(code, detail string) *HTTPError
- func InternalServer(code, detail string) *HTTPError
- func MethodNotAllowed(code, detail string) *HTTPError
- func NewHTTPError(status int, code, detail string) *HTTPError
- func NewHTTPErrorWithCause(status int, code, detail string, cause error) *HTTPError
- func NormalizeError(err error) *HTTPError
- func NotFound(code, detail string) *HTTPError
- func TooManyRequests(code, detail string) *HTTPError
- func Unauthorized(code, detail string) *HTTPError
- func UnprocessableEntity(code, detail string) *HTTPError
Constants ¶
const ( CodeInvalid = reqx.CodeInvalid CodeRequired = reqx.CodeRequired CodeUnknown = reqx.CodeUnknown CodeType = reqx.CodeType CodeMultiple = reqx.CodeMultiple )
Variables ¶
This section is empty.
Functions ¶
func Accepted ¶ added in v0.8.4
func Accepted(w http.ResponseWriter, data any) error
Accepted 写回 202 成功响应。
func Created ¶ added in v0.2.0
func Created(w http.ResponseWriter, data any) error
Created 写回 201 成功响应。
func InvalidRequest ¶ added in v0.1.1
func InvalidRequest(fieldErrors ...FieldError) error
InvalidRequest 生成统一的 invalid_request 错误包络。
func JSON ¶ added in v0.2.0
func JSON(w http.ResponseWriter, status int, data any) error
JSON 写回 JSON 响应。
func NoContent ¶ added in v0.2.0
func NoContent(w http.ResponseWriter) error
NoContent 写回 204 无响应体成功响应。
func Query ¶ added in v0.4.1
func Query(r *http.Request, name string) *reqx.QueryParam
Query 创建 query 单参数读取与校验 builder。
func WriteError ¶
func WriteError(w http.ResponseWriter, err error, topCode ...int) error
WriteError 按统一错误对象写回响应。
Types ¶
type HTTPError ¶
HTTPError 表示 HTTP 边界上的公共错误。
func BadRequest ¶
BadRequest 构造 400 Bad Request 公共错误。
func InternalServer ¶ added in v0.8.3
InternalServer 构造 500 Internal Server Error 公共错误。
func MethodNotAllowed ¶
MethodNotAllowed 构造 405 Method Not Allowed 公共错误。
func NewHTTPError ¶
NewHTTPError 构造一个不带底层 cause 的公共 HTTP 错误。
func NewHTTPErrorWithCause ¶ added in v0.5.2
NewHTTPErrorWithCause 基于给定 cause 构造公共 HTTP 错误。
func TooManyRequests ¶
TooManyRequests 构造 429 Too Many Requests 公共错误。
func Unauthorized ¶
Unauthorized 构造 401 Unauthorized 公共错误。
func UnprocessableEntity ¶
UnprocessableEntity 构造 422 Unprocessable Entity 公共错误。
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
errx
Package errx 提供共享 HTTP 错误模型的内部实现。
|
Package errx 提供共享 HTTP 错误模型的内部实现。 |
|
resp
Package resp 为基于 net/http 的 JSON API 提供响应侧辅助能力的内部实现。
|
Package resp 为基于 net/http 的 JSON API 提供响应侧辅助能力的内部实现。 |
|
Package reqx 为基于 net/http 的 JSON API 提供输入侧能力。
|
Package reqx 为基于 net/http 的 JSON API 提供输入侧能力。 |