Documentation
¶
Overview ¶
Package gin provides gin-integrated helpers for the response package. It bridges response.AppError / response.Response with gin.Context, resolving i18n messages via an optional i18n.Manager.
Usage:
// In your handler:
data, err := service.GetUser(id)
if err != nil {
gin.WriteError(c, err)
return
}
gin.Success(c, data)
// With i18n:
gin.SuccessI18n(c, response.KeySuccess, data)
gin.FailI18n(c, response.KeyNotFound, nil)
Index ¶
- Variables
- func AbortWithStatusJSON(c *gin.Context, httpStatus int, err error)
- func Created(c *gin.Context, data any)
- func Fail(c *gin.Context, msg string, data any)
- func FailAppError(c *gin.Context, ae *response.AppError)
- func FailI18n(c *gin.Context, key string, data any, args ...any)
- func FailWithCode(c *gin.Context, code response.Code, msg string, data any)
- func HandleError(c *gin.Context, err error)
- func NoContent(c *gin.Context)
- func Recovery() gin.HandlerFunc
- func Success(c *gin.Context, data any)
- func SuccessI18n(c *gin.Context, key string, data any, args ...any)
- func SuccessMsg(c *gin.Context, msg string, data any)
- func WriteError(c *gin.Context, err error)
Constants ¶
This section is empty.
Variables ¶
var Resolver response.MessageResolver
Resolver is the package-level MessageResolver used by the gin helpers. Set this at startup (typically wrapping an i18n.Manager). If nil, the NoopResolver is used (keys returned as-is).
var manager *i18n.Manager = ...
gin.Resolver = gin.ResolverFunc(func(key string, args ...any) string {
locale := gin.GetLocaleFromContext(...) // or your middleware
return manager.T(locale, key, args...)
})
Functions ¶
func AbortWithStatusJSON ¶
AbortWithStatusJSON aborts the request with an error envelope derived from the HTTP status and error. The HTTP status is 200 so callers can uniformly inspect the JSON `code` field; the business error code is in the envelope. The httpStatus argument is used only to derive the business code.
func Fail ¶
Fail writes an error envelope with a direct message. The HTTP status is 200 so callers can uniformly inspect the JSON `code` field; the business error code is embedded in the envelope.
func FailAppError ¶
FailAppError writes a typed AppError directly.
func FailI18n ¶
FailI18n writes an error envelope with a localized message derived from the i18n key. The HTTP status is 200; the business code is in the envelope.
func FailWithCode ¶
FailWithCode writes an error envelope with a custom numeric code and message. The HTTP status is 200; the business code is in the envelope.
func HandleError ¶
HandleError is an alias for WriteError.
func Recovery ¶
func Recovery() gin.HandlerFunc
Recovery returns a gin middleware that recovers from panics and writes a 500 error envelope. It should be registered early.
func SuccessI18n ¶
SuccessI18n writes a 200 success envelope with a localized message.
func SuccessMsg ¶
SuccessMsg writes a 200 success envelope with a custom message.
func WriteError ¶
WriteError renders any error as an error envelope. If err is already an *AppError it is used directly; otherwise it is wrapped as CodeInternal. The HTTP status is 200 so callers can uniformly inspect the JSON `code` field; the business error code is in the envelope.
Types ¶
This section is empty.