httpx

package
v0.1.59 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthMethodFrom

func AuthMethodFrom(c echo.Context) string

AuthMethodFrom 返回当前请求的鉴权方式:'jwt' / 'user_token' / ”(未鉴权)。 HybridAuthMiddleware 命中后设置。

func HasScope

func HasScope(c echo.Context, expected string) bool

HasScope reports whether the authenticated User Token carries a required permission.

func IsAdmin

func IsAdmin(c echo.Context) bool

IsAdmin 是否管理员。

func SendError

func SendError(c echo.Context, err error) error

SendError 输出标准化错误响应。

func UserIDFrom

func UserIDFrom(c echo.Context) string

UserIDFrom 从 echo.Context 取出 user_id(鉴权中间件设置)。

Types

type CtxKey

type CtxKey string

CtxKey 类型安全的 context key。

const (
	CtxKeyUserID        CtxKey = "user_id"
	CtxKeyAdmin         CtxKey = "is_admin"
	CtxKeyAuthMethod    CtxKey = "auth_method"
	CtxKeyAuthScopes    CtxKey = "auth_scopes"
	CtxKeyAuthPrincipal CtxKey = "auth_principal"
)

type ErrorBody

type ErrorBody struct {
	Code    ErrorCode `json:"code"`
	Message string    `json:"message"`
	Details any       `json:"details,omitempty"`
}

ErrorBody 错误正文。

type ErrorCode

type ErrorCode string

ErrorCode 标准化错误码(见 docs/13 附录 A)。

const (
	CodeBadRequest                          ErrorCode = "BAD_REQUEST"
	CodeUnauthorized                        ErrorCode = "UNAUTHORIZED"
	CodeForbidden                           ErrorCode = "FORBIDDEN"
	CodePermissionDenied                    ErrorCode = "PERMISSION_DENIED"
	CodePermissionExpansionRequiresNewToken ErrorCode = "PERMISSION_EXPANSION_REQUIRES_NEW_TOKEN"
	CodeNotFound                            ErrorCode = "NOT_FOUND"
	CodeConflict                            ErrorCode = "CONFLICT"
	CodeUnprocessable                       ErrorCode = "VALIDATION_FAILED"
	CodeRateLimited                         ErrorCode = "RATE_LIMITED"
	CodeInternal                            ErrorCode = "INTERNAL_ERROR"
	CodeServiceUnavailable                  ErrorCode = "SERVICE_UNAVAILABLE"
)

type ErrorResponse

type ErrorResponse struct {
	Error ErrorBody `json:"error"`
}

ErrorResponse 统一错误响应体。

{ "error": { "code": "...", "message": "..." } }

type HTTPError

type HTTPError struct {
	Status  int
	Code    ErrorCode
	Message string
	Details any
}

HTTPError 业务级 HTTP 错误。

func BadRequest

func BadRequest(msg string) *HTTPError

func Conflict

func Conflict(msg string) *HTTPError

func Forbidden

func Forbidden(msg string) *HTTPError

func Internal

func Internal(msg string) *HTTPError

func NewError

func NewError(status int, code ErrorCode, message string) *HTTPError

NewError 构造业务错误。

func NotFound

func NotFound(msg string) *HTTPError

func PermissionDenied added in v0.1.41

func PermissionDenied(permission string, resource ...string) *HTTPError

func RateLimited

func RateLimited(msg string) *HTTPError

func ServiceUnavailable

func ServiceUnavailable(msg string) *HTTPError

func Unauthorized

func Unauthorized(msg string) *HTTPError

func Unprocessable

func Unprocessable(msg string) *HTTPError

func (*HTTPError) Error

func (e *HTTPError) Error() string

Jump to

Keyboard shortcuts

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