httpx

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package httpx provides HTTP utilities for gorp framework. This file exposes the standard HTTP response protocol and error types. Business handlers use these types to build consistent HTTP responses.

httpx 包提供 gorp 框架的 HTTP 工具。 本文件暴露标准 HTTP 响应协议和错误类型。 业务 handler 使用这些类型构建一致的 HTTP 响应。

Index

Constants

View Source
const (
	CodeBadRequest    = 1001 // 请求参数错误
	CodeUnauthorized  = 1002 // 未认证
	CodeForbidden     = 1003 // 无权限
	CodeNotFound      = 1004 // 资源不存在
	CodeInternalError = 1005 // 内部错误
)

业务错误码常量,与 HTTP 状态码对应但独立编码。 Business error code constants, mapped to HTTP status codes but independently encoded.

View Source
const CodeSuccess = 0

CodeSuccess is the success response code.

CodeSuccess 是成功响应码。

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(c transportcontract.Context, message string)

BadRequest writes a 400 Bad Request response with a business error code and message.

BadRequest 输出 400 Bad Request 响应,附带业务错误码和消息。

func Error

func Error(c transportcontract.Context, err error)

Error writes an error response.

Error 输出错误响应。

func ErrorWithData

func ErrorWithData(c transportcontract.Context, err error, data any)

ErrorWithData writes an error response and attaches extra response data.

ErrorWithData 输出错误响应,并附带额外数据。

func ErrorWithStatus

func ErrorWithStatus(c transportcontract.Context, status int, err error)

ErrorWithStatus writes an error response using a caller-provided HTTP status.

ErrorWithStatus 使用调用方指定的 HTTP 状态码输出错误响应。

func Forbidden

func Forbidden(c transportcontract.Context, message string)

Forbidden writes a 403 Forbidden response with a business error code and message.

Forbidden 输出 403 Forbidden 响应,附带业务错误码和消息。

func InternalError

func InternalError(c transportcontract.Context, message string)

InternalError writes a 500 Internal Server Error response with a business error code and message.

InternalError 输出 500 Internal Server Error 响应,附带业务错误码和消息。

func NotFound

func NotFound(c transportcontract.Context, message string)

NotFound writes a 404 Not Found response with a business error code and message.

NotFound 输出 404 Not Found 响应,附带业务错误码和消息。

func Success

func Success(c transportcontract.Context, data any)

Success writes a standard success response.

Success 输出标准成功响应。

func SuccessPaginated

func SuccessPaginated(c transportcontract.Context, items any, total int64, page, pageSize int)

SuccessPaginated writes a standard paginated success response.

SuccessPaginated 输出标准的分页成功响应。

func SuccessWithMessage

func SuccessWithMessage(c transportcontract.Context, message string, data any)

SuccessWithMessage writes a success response with a custom message.

SuccessWithMessage 输出带自定义消息的成功响应。

func SuccessWithStatus

func SuccessWithStatus(c transportcontract.Context, status int, data any)

SuccessWithStatus writes a success response with a custom HTTP status.

SuccessWithStatus 输出带自定义 HTTP 状态码的成功响应。

func Unauthorized

func Unauthorized(c transportcontract.Context, message string)

Unauthorized writes a 401 Unauthorized response with a business error code and message.

Unauthorized 输出 401 Unauthorized 响应,附带业务错误码和消息。

Types

type BizError

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

BizError is the default business error implementation.

BizError 是默认的业务错误实现。

func NewBizError

func NewBizError(code int, message string) *BizError

NewBizError creates a new business error with the given code and message.

NewBizError 使用给定错误码和消息创建业务错误。

func (*BizError) Code

func (e *BizError) Code() int

func (*BizError) Error

func (e *BizError) Error() string

func (*BizError) Message

func (e *BizError) Message() string

type BusinessError

type BusinessError interface {
	error
	Code() int
	Message() string
}

BusinessError describes an error carrying business code and message semantics.

BusinessError 描述带有业务错误码与消息语义的错误。

type PaginatedData

type PaginatedData struct {
	Items    any   `json:"items"`
	Total    int64 `json:"total"`
	Page     int   `json:"page"`
	PageSize int   `json:"page_size"`
}

PaginatedData is the standard pagination payload shape.

PaginatedData 是标准的分页载荷结构。

type Response

type Response struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

Response is the standard HTTP response envelope used by the framework.

Response 是框架使用的标准 HTTP 响应包裹结构。

Jump to

Keyboard shortcuts

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