gin

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

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

func AbortWithStatusJSON(c *gin.Context, httpStatus int, err error)

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 Created

func Created(c *gin.Context, data any)

Created writes a 201 success envelope (resource created).

func Fail

func Fail(c *gin.Context, msg string, data any)

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

func FailAppError(c *gin.Context, ae *response.AppError)

FailAppError writes a typed AppError directly.

func FailI18n

func FailI18n(c *gin.Context, key string, data any, args ...any)

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

func FailWithCode(c *gin.Context, code response.Code, msg string, data any)

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

func HandleError(c *gin.Context, err error)

HandleError is an alias for WriteError.

func NoContent

func NoContent(c *gin.Context)

NoContent writes a 204 with no body.

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 Success

func Success(c *gin.Context, data any)

Success writes a 200 success envelope.

func SuccessI18n

func SuccessI18n(c *gin.Context, key string, data any, args ...any)

SuccessI18n writes a 200 success envelope with a localized message.

func SuccessMsg

func SuccessMsg(c *gin.Context, msg string, data any)

SuccessMsg writes a 200 success envelope with a custom message.

func WriteError

func WriteError(c *gin.Context, err error)

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.

Jump to

Keyboard shortcuts

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