Documentation
¶
Index ¶
- Constants
- Variables
- func InitBasicLocalizationData()
- func ParseBody(c *gin.Context, data any) error
- func ParseQuery(c *gin.Context, data any) error
- func SetLocalizationData(jsonString string)
- func WrapHandler(fn func(ctx *gin.Context) error) gin.HandlerFunc
- func WriteResponse(c *gin.Context, statusCode int, localCode cwsbase.LocalizationCode, data any, ...)
- func WriteResponseWithMongoCursor[T any](c *gin.Context, statusCode int, localCode cwsbase.LocalizationCode, ...) error
- type CWSLocalizedErrorResponse
- func (r CWSLocalizedErrorResponse) EmbedError(err error) CWSLocalizedErrorResponse
- func (r CWSLocalizedErrorResponse) Error() string
- func (r CWSLocalizedErrorResponse) MessageValues(values ...any) CWSLocalizedErrorResponse
- func (r CWSLocalizedErrorResponse) ToMessage() string
- func (r CWSLocalizedErrorResponse) WriteResponse(ctx *gin.Context)
- type CWSLocalizedResponse
Constants ¶
const ( // LocalCode_InternalServerError represents HTTP 500 status code LocalCode_InternalServerError cwsbase.LocalizationCode = "500" // LocalCode_BadRequest represents HTTP 400 status code LocalCode_BadRequest cwsbase.LocalizationCode = "400" LocalCode_Unauthorized cwsbase.LocalizationCode = "401" // LocalCode_OK represents HTTP 200 status code LocalCode_OK cwsbase.LocalizationCode = "200" // LocalCode_Forbidden represents HTTP 403 status code LocalCode_Forbidden cwsbase.LocalizationCode = "403" // LocalCode_NotFound represents HTTP 404 status code LocalCode_NotFound cwsbase.LocalizationCode = "404" )
HTTP status code constants for localization
Variables ¶
var BadRequestErrorResponse = CWSLocalizedErrorResponse{ StatusCode: http.StatusBadRequest, LocalCode: LocalCode_BadRequest, }
BadRequestErrorResponse represents a pre-configured 400 Bad Request error response
var ForbiddenErrorResponse = CWSLocalizedErrorResponse{ StatusCode: http.StatusForbidden, LocalCode: LocalCode_Forbidden, }
ForbiddenErrorResponse represents a pre-configured 403 Forbidden error response
var InternalServerErrorResponse = CWSLocalizedErrorResponse{ StatusCode: http.StatusInternalServerError, LocalCode: LocalCode_InternalServerError, }
InternalServerErrorResponse represents a pre-configured 500 Internal Server Error response
var NotFoundErrorResponse = CWSLocalizedErrorResponse{ StatusCode: http.StatusNotFound, LocalCode: LocalCode_NotFound, }
NotFoundErrorResponse represents a pre-configured 404 Not Found error response
var OKResponse = CWSLocalizedResponse{ StatusCode: http.StatusOK, LocalCode: LocalCode_OK, }
OKResponse represents a pre-configured 200 OK success response
StatusCode: http.StatusUnauthorized, LocalCode: LocalCode_Unauthorized, }
UnauthorizedErrorResponse represents a pre-configured 401 Unauthorized error response
Functions ¶
func InitBasicLocalizationData ¶ added in v0.3.1
func InitBasicLocalizationData()
InitBasicLocalizationData initializes the localization system with default multilingual HTTP status messages This function should be called during application startup to load standard error messages
func ParseBody ¶
ParseBody parses the HTTP request body into the provided data structure using Gin's ShouldBind Supports JSON, XML, YAML, and form data binding based on Content-Type header Returns a CWSLocalizedErrorResponse with 400 Bad Request status if parsing fails
func ParseQuery ¶ added in v0.3.1
ParseQuery parses the HTTP query parameters into the provided data structure using Gin's ShouldBindQuery Automatically converts query string parameters to the appropriate struct fields based on struct tags Returns a CWSLocalizedErrorResponse with 400 Bad Request status if parsing fails
func SetLocalizationData ¶
func SetLocalizationData(jsonString string)
SetLocalizationData updates the localization data with custom JSON string This allows applications to provide their own localized messages
func WrapHandler ¶
func WrapHandler(fn func(ctx *gin.Context) error) gin.HandlerFunc
WrapHandler wraps a function that returns an error into a standard Gin handler This provides unified error handling for all HTTP handlers in the application If the error is a CWSLocalizedErrorResponse, it writes the localized response Otherwise, it writes a 500 Internal Server Error and panics to ensure the error is logged
func WriteResponse ¶
func WriteResponse(c *gin.Context, statusCode int, localCode cwsbase.LocalizationCode, data any, localEmbeddingStrs ...any)
WriteResponse writes a standardized JSON response with localized message The response format includes code, message, and data fields Parameters:
- c: Gin context for writing the HTTP response
- statusCode: HTTP status code to return
- localCode: Localization code for retrieving the appropriate message
- data: Response payload data
- localEmbeddingStrs: Optional values to embed in the localized message using sprintf formatting
func WriteResponseWithMongoCursor ¶
func WriteResponseWithMongoCursor[T any](c *gin.Context, statusCode int, localCode cwsbase.LocalizationCode, cursor *mongo.Cursor, localEmbeddingStrs ...any) error
WriteResponseWithMongoCursor streams MongoDB cursor data as a JSON response without loading all data into memory This is useful for large datasets as it streams data directly from MongoDB to the HTTP response The response is written incrementally, reducing memory usage for large result sets Generic type T represents the data type being streamed from the MongoDB cursor Parameters:
- c: Gin context for writing the HTTP response
- statusCode: HTTP status code to return
- localCode: Localization code for retrieving the appropriate message
- cursor: MongoDB cursor containing the data to stream
- localEmbeddingStrs: Optional values to embed in the localized message using sprintf formatting
Returns an error if cursor iteration or JSON encoding fails
Types ¶
type CWSLocalizedErrorResponse ¶ added in v0.3.7
type CWSLocalizedErrorResponse struct {
// StatusCode represents the HTTP status code to be returned
StatusCode int
// LocalCode is the localization key for retrieving localized messages
LocalCode cwsbase.LocalizationCode
// contains filtered or unexported fields
}
CWSLocalizedErrorResponse represents a localized HTTP response structure that implements the error interface It contains status code, localization code, embedded values for message formatting, and actual error details
func (CWSLocalizedErrorResponse) EmbedError ¶ added in v0.3.15
func (r CWSLocalizedErrorResponse) EmbedError(err error) CWSLocalizedErrorResponse
EmbedError sets the actual error to be included in debug mode responses The actual error is only shown when DEBUG environment variable is set to true Returns the same CWSLocalizedErrorResponse instance for method chaining
func (CWSLocalizedErrorResponse) Error ¶ added in v0.3.7
func (r CWSLocalizedErrorResponse) Error() string
Error implements the error interface for CWSLocalizedErrorResponse Returns the localized error message, optionally including actual error details in debug mode
func (CWSLocalizedErrorResponse) MessageValues ¶ added in v0.3.15
func (r CWSLocalizedErrorResponse) MessageValues(values ...any) CWSLocalizedErrorResponse
MessageValues sets the values to be embedded in the localized error message using sprintf formatting Returns the same CWSLocalizedErrorResponse instance for method chaining
func (CWSLocalizedErrorResponse) ToMessage ¶ added in v0.3.15
func (r CWSLocalizedErrorResponse) ToMessage() string
ToMessage returns the localized message for this error response
func (CWSLocalizedErrorResponse) WriteResponse ¶ added in v0.3.15
func (r CWSLocalizedErrorResponse) WriteResponse(ctx *gin.Context)
WriteResponse writes the HTTP error response to the gin context in JSON format Automatically handles common database errors and debug mode error details
type CWSLocalizedResponse ¶ added in v0.3.6
type CWSLocalizedResponse struct {
// StatusCode represents the HTTP status code to be returned
StatusCode int
// LocalCode is the localization key for retrieving localized messages
LocalCode cwsbase.LocalizationCode
// contains filtered or unexported fields
}
CWSLocalizedResponse represents a localized HTTP response structure It contains status code, localization code, embedded values for message formatting, and response data
func (CWSLocalizedResponse) MessageValues ¶ added in v0.3.15
func (r CWSLocalizedResponse) MessageValues(values ...any) CWSLocalizedResponse
MessageValues sets the values to be embedded in the localized message using sprintf formatting Returns the same CWSLocalizedResponse instance for method chaining
func (CWSLocalizedResponse) ResponseData ¶ added in v0.3.15
func (r CWSLocalizedResponse) ResponseData(data any) CWSLocalizedResponse
ResponseData sets the data to be included in the response Returns the same CWSLocalizedResponse instance for method chaining
func (CWSLocalizedResponse) ToMessage ¶ added in v0.3.15
func (r CWSLocalizedResponse) ToMessage() string
ToMessage returns the localized message for this response
func (CWSLocalizedResponse) WriteResponse ¶ added in v0.3.15
func (r CWSLocalizedResponse) WriteResponse(ctx *gin.Context)
WriteResponse writes the HTTP response to the gin context in JSON format