Documentation
¶
Index ¶
- Constants
- Variables
- func BadRequest(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func BindError(w http.ResponseWriter, r *http.Request, details any, opts ...Option)
- func Conflict(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func Created(w http.ResponseWriter, r *http.Request, data any, opts ...Option)
- func CustomError(w http.ResponseWriter, r *http.Request, status int, code int, message string, ...)
- func DatabaseError(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func DefaultPanicFn(w http.ResponseWriter, r *http.Request, err error)
- func Forbidden(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func GetErrorMessage(code int) string
- func InternalServerError(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func NoContent(w http.ResponseWriter, r *http.Request, opts ...Option)
- func NotFound(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func OK(w http.ResponseWriter, r *http.Request, data any, opts ...Option)
- func ServiceUnavailable(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func TooManyRequests(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func Unauthorized(w http.ResponseWriter, r *http.Request, message string, opts ...Option)
- func ValidationError(w http.ResponseWriter, r *http.Request, details any, opts ...Option)
- func Write(w http.ResponseWriter, r *http.Request, status int, data any, opts ...Option)
- func WriteError(w http.ResponseWriter, r *http.Request, status int, err Error, opts ...Option)
- func WriteList(w http.ResponseWriter, r *http.Request, status int, data any, ...)
- type Error
- type ErrorConfig
- type FactoryOption
- type FieldError
- type Meta
- type Option
- type PaginationMeta
- type PanicFn
- type Responder
- func (r *Responder) BadRequest(message string, opts ...Option)
- func (r *Responder) BindError(details any, opts ...Option)
- func (r *Responder) Conflict(message string, opts ...Option)
- func (r *Responder) Created(data any, opts ...Option)
- func (r *Responder) CustomError(status int, code int, message string, details any, opts ...Option)
- func (r *Responder) DatabaseError(message string, opts ...Option)
- func (r *Responder) Forbidden(message string, opts ...Option)
- func (r *Responder) GetCustomError(code int) *ErrorConfig
- func (r *Responder) InternalServerError(message string, opts ...Option)
- func (r *Responder) NoContent(opts ...Option)
- func (r *Responder) NotFound(message string, opts ...Option)
- func (r *Responder) OK(data any, opts ...Option)
- func (r *Responder) ServiceUnavailable(message string, opts ...Option)
- func (r *Responder) TooManyRequests(message string, opts ...Option)
- func (r *Responder) Unauthorized(message string, opts ...Option)
- func (r *Responder) ValidationError(details any, opts ...Option)
- func (r *Responder) Write(status int, payload any, opts ...Option)
- func (r *Responder) WriteError(status int, err Error, opts ...Option)
- func (r *Responder) WriteList(status int, payload any, pager *PaginationMeta, opts ...Option)
- type ResponderFactory
- type Response
Constants ¶
const ( // 4xxx - 客户端错误 ErrCodeBadRequest = 4000 // 请求格式错误 ErrCodeBindFailed = 4001 // 参数绑定错误 ErrCodeValidationFailed = 4002 // 数据验证失败 ErrCodeNotFound = 4003 // 资源不存在 ErrCodeRouteNotFound = 4004 // 路由不存在 ErrCodeForbidden = 4005 // 权限不足 ErrCodeDuplicate = 4007 // 资源已存在 ErrCodeConflict = 4008 // 数据冲突 ErrCodeTooManyRequests = 4009 // 请求过于频繁 ErrCodeRateLimitExceeded = 4009 // 速率限制超出 (别名) // 5xxx - 服务端错误 ErrCodeInternalServer = 5000 // 内部服务器错误 ErrCodeDatabase = 5001 // 数据库错误 ErrCodeBusinessLogic = 5002 // 业务逻辑错误 ErrCodeFileUpload = 5003 // 文件上传失败 ErrCodeStorageService = 5004 // 存储服务错误 ErrCodeExternalService = 5005 // 外部服务错误 ErrCodeTimeout = 5006 // 请求超时 )
HTTP 状态码相关的错误码
Variables ¶
var ( ErrBadRequest = NewError(ErrCodeBadRequest, "") ErrBindFailed = NewError(ErrCodeBindFailed, "") ErrValidationFailed = NewError(ErrCodeValidationFailed, "") ErrNotFound = NewError(ErrCodeNotFound, "") ErrRouteNotFound = NewError(ErrCodeRouteNotFound, "") ErrForbidden = NewError(ErrCodeForbidden, "") ErrDuplicate = NewError(ErrCodeDuplicate, "") ErrConflict = NewError(ErrCodeConflict, "") ErrTooManyRequests = NewError(ErrCodeTooManyRequests, "") ErrInternalServer = NewError(ErrCodeInternalServer, "") ErrDatabase = NewError(ErrCodeDatabase, "") ErrBusinessLogic = NewError(ErrCodeBusinessLogic, "") ErrFileUpload = NewError(ErrCodeFileUpload, "") ErrStorageService = NewError(ErrCodeStorageService, "") ErrExternalService = NewError(ErrCodeExternalService, "") ErrTimeout = NewError(ErrCodeTimeout, "") )
Predefined errors for common scenarios
Functions ¶
func BadRequest ¶
BadRequest responds with 400 Bad Request
func CustomError ¶
func CustomError(w http.ResponseWriter, r *http.Request, status int, code int, message string, details any, opts ...Option)
CustomError responds with custom status code and error
func DatabaseError ¶
DatabaseError responds with 500 Internal Server Error for database errors
func DefaultPanicFn ¶
func DefaultPanicFn(w http.ResponseWriter, r *http.Request, err error)
func GetErrorMessage ¶
GetErrorMessage returns the default message for an error code
func InternalServerError ¶
InternalServerError responds with 500 Internal Server Error
func NoContent ¶
func NoContent(w http.ResponseWriter, r *http.Request, opts ...Option)
NoContent responds with 204 No Content
func ServiceUnavailable ¶
ServiceUnavailable responds with 503 Service Unavailable
func TooManyRequests ¶
TooManyRequests responds with 429 Too Many Requests
func Unauthorized ¶
Unauthorized responds with 401 Unauthorized
func ValidationError ¶
ValidationError responds with 400 Bad Request and validation details Accepts any type of details ([]FieldError, map[string]string, or custom struct)
func WriteError ¶
WriteError sends an error response
func WriteList ¶
func WriteList(w http.ResponseWriter, r *http.Request, status int, data any, pager *PaginationMeta, opts ...Option)
WriteList sends a success response with data and pagination
Types ¶
type Error ¶
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Details any `json:"details,omitempty"`
}
Error represents the error structure in API responses
type ErrorConfig ¶
type ErrorConfig struct {
Code int // Error code (e.g., 4001, 5001)
Message string // Default error message
HTTPStatus int // HTTP status code (e.g., 400, 500)
}
ErrorConfig defines custom error code configuration
type FactoryOption ¶
type FactoryOption func(*ResponderFactory)
FactoryOption defines configuration options for ResponderFactory
func WithCustomErrors ¶
func WithCustomErrors(errors map[int]*ErrorConfig) FactoryOption
WithCustomErrors adds custom error code mappings Example:
WithCustomErrors(map[int]*ErrorConfig{
4100: {Code: 4100, Message: "User not found", HTTPStatus: 404},
5100: {Code: 5100, Message: "Payment service error", HTTPStatus: 500},
})
func WithPanicFn ¶
func WithPanicFn(panicFn PanicFn) FactoryOption
WithPanicFn sets a custom panic handler
type FieldError ¶
FieldError represents a validation error for a specific field
type Meta ¶
type Meta struct {
TraceId string `json:"traceId,omitempty"`
Took int64 `json:"took,omitempty"`
Pagination *PaginationMeta `json:"pagination,omitempty"`
}
Meta represents metadata in API responses
type Option ¶
type Option func(*Meta)
func WithPagination ¶
func WithPagination(p *PaginationMeta) Option
func WithTraceID ¶
type PaginationMeta ¶
type PaginationMeta struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
Total int64 `json:"total"`
TotalPages int `json:"totalPages"`
HasMore bool `json:"hasMore"`
}
PaginationMeta represents pagination information
type Responder ¶
type Responder struct {
// contains filtered or unexported fields
}
func (*Responder) BadRequest ¶
BadRequest responds with 400 Bad Request
func (*Responder) CustomError ¶
CustomError responds with custom status code and error
func (*Responder) DatabaseError ¶
DatabaseError responds with 500 Internal Server Error for database errors
func (*Responder) GetCustomError ¶
func (r *Responder) GetCustomError(code int) *ErrorConfig
GetCustomError retrieves custom error configuration by code
func (*Responder) InternalServerError ¶
InternalServerError responds with 500 Internal Server Error
func (*Responder) ServiceUnavailable ¶
ServiceUnavailable responds with 503 Service Unavailable
func (*Responder) TooManyRequests ¶
TooManyRequests responds with 429 Too Many Requests
func (*Responder) Unauthorized ¶
Unauthorized responds with 401 Unauthorized
func (*Responder) ValidationError ¶
ValidationError responds with 400 Bad Request and validation details Accepts []FieldError for detailed field-level errors
func (*Responder) WriteError ¶
WriteError sends an error response
type ResponderFactory ¶
type ResponderFactory struct {
// contains filtered or unexported fields
}
func NewResponderFactory ¶
func NewResponderFactory(opts ...FactoryOption) *ResponderFactory
NewResponderFactory creates a new ResponderFactory with options
func (*ResponderFactory) FromRequest ¶
func (f *ResponderFactory) FromRequest(w http.ResponseWriter, r *http.Request) *Responder