Documentation
¶
Index ¶
- func BadRequest(c *fiber.Ctx, message string) error
- func BadRequestI18n(c *fiber.Ctx, messageID string, data interface{}) error
- func Error(c *fiber.Ctx, code int, message string) error
- func ErrorI18n(c *fiber.Ctx, code int, messageID string, data interface{}) error
- func FiberErrorHandler(ctx *fiber.Ctx, err error) error
- func NotFound(c *fiber.Ctx, message string) error
- func NotFoundI18n(c *fiber.Ctx, messageID string) error
- func SetI18nManager(manager *i18n.I18nManager)
- func Success(c *fiber.Ctx, message string, data interface{}) error
- func SuccessI18n(c *fiber.Ctx, messageID string, data interface{}) error
- func SuccessWithPagination(c *fiber.Ctx, message string, data PaginationResult) error
- func SuccessWithPaginationI18n(c *fiber.Ctx, messageID string, data PaginationResult) error
- func ValidationErrorI18n(c *fiber.Ctx, err error) error
- type PaginationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BadRequest ¶
BadRequest returns a 400 Bad Request JSON response with the specified message.
Response format:
{
"meta": {
"success": false,
"message": "Invalid request"
},
"data": null
}
Parameters:
- c: *fiber.Ctx - The Fiber context
- message: Error message to include in response
Returns:
- error: Fiber error for response handling
Example:
return response.BadRequest(c, "Invalid email format")
func BadRequestI18n ¶
BadRequestI18n returns a 400 Bad Request response with a translated message. The message is translated based on the language from the request context. Supports template data for dynamic message interpolation.
Parameters:
- c: *fiber.Ctx - The Fiber context
- messageID: Message identifier to translate
- data: Template data for message interpolation (can be nil)
Returns:
- error: Fiber error for response handling
Example:
return response.BadRequestI18n(c, "invalid_email", map[string]string{
"Email": "invalid@",
})
func Error ¶
Error returns a JSON error response with the specified status code and message.
Response format:
{
"meta": {
"success": false,
"message": "Error message"
},
"data": null
}
Parameters:
- c: *fiber.Ctx - The Fiber context
- code: HTTP status code (e.g., 400, 404, 500)
- message: Error message to include in response
Returns:
- error: Fiber error for response handling
Example:
return response.Error(c, 500, "Internal server error")
func ErrorI18n ¶
ErrorI18n returns an error response with a translated message and custom status code. The message is translated based on the language from the request context. Supports template data for dynamic message interpolation.
Parameters:
- c: *fiber.Ctx - The Fiber context
- code: HTTP status code
- messageID: Message identifier to translate
- data: Template data for message interpolation (can be nil)
Returns:
- error: Fiber error for response handling
Example:
return response.ErrorI18n(c, 500, "database_error", map[string]string{
"Table": "users",
})
func FiberErrorHandler ¶
FiberErrorHandler is a custom error handler for Fiber framework that processes errors and returns internationalized error responses.
It handles different HTTP status codes and returns appropriate i18n error responses:
- 404 (Not Found): Returns NotFoundI18n response
- 400 (Bad Request): Returns BadRequestI18n response
- Other status codes: Returns ErrorI18n response with the corresponding status code
If the error is a *fiber.Error, it uses the error's status code. Otherwise, it defaults to 500 (Internal Server Error).
Parameters:
- ctx: The Fiber context containing the request/response data
- err: The error to be handled and formatted
Returns:
- error: An internationalized error response based on the status code
func NotFound ¶
NotFound returns a 404 Not Found JSON response with the specified message.
Response format:
{
"meta": {
"success": false,
"message": "Resource not found"
},
"data": null
}
Parameters:
- c: *fiber.Ctx - The Fiber context
- message: Error message to include in response
Returns:
- error: Fiber error for response handling
Example:
return response.NotFound(c, "User not found")
func NotFoundI18n ¶
NotFoundI18n returns a 404 Not Found response with a translated message. The message is translated based on the language from the request context. If i18nManager is not set, it falls back to using the messageID as the message.
Parameters:
- c: *fiber.Ctx - The Fiber context
- messageID: Message identifier to translate
Returns:
- error: Fiber error for response handling
Example:
return response.NotFoundI18n(c, "user_not_found")
func SetI18nManager ¶
func SetI18nManager(manager *i18n.I18nManager)
SetI18nManager sets the global I18nManager instance to be used by i18n response functions. This function must be called during application initialization before using any i18n response methods.
Parameters:
- manager: *i18n.I18nManager - The initialized i18n manager instance
Example:
i18nMgr, _ := i18n.NewI18nManager(config) response.SetI18nManager(i18nMgr)
func Success ¶
Success returns a 200 OK JSON response with the specified message and data.
Response format:
{
"meta": {
"success": true,
"message": "Success message"
},
"data": {
// your data here
}
}
Parameters:
- c: *fiber.Ctx - The Fiber context
- message: Success message to include in response
- data: Response data (can be nil, struct, map, slice, etc.)
Returns:
- error: Fiber error for response handling
Example:
return response.Success(c, "User created successfully", fiber.Map{
"id": 123,
"name": "John Doe",
})
func SuccessI18n ¶
SuccessI18n returns a 200 OK response with a translated message and optional data. The message is translated based on the language from the request context.
Parameters:
- c: *fiber.Ctx - The Fiber context
- messageID: Message identifier to translate
- data: Response data to include in the response body (can be nil)
Returns:
- error: Fiber error for response handling
Example:
return response.SuccessI18n(c, "user_created", fiber.Map{
"id": 123,
"name": "John Doe",
})
func SuccessWithPagination ¶ added in v1.0.5
func SuccessWithPagination(c *fiber.Ctx, message string, data PaginationResult) error
func SuccessWithPaginationI18n ¶ added in v1.0.5
func SuccessWithPaginationI18n(c *fiber.Ctx, messageID string, data PaginationResult) error
func ValidationErrorI18n ¶
ValidationErrorI18n returns a 400 Bad Request response with validation error details. It extracts field-specific errors from the ValidationError and formats them in a JSON response. If the error is not a ValidationError, it falls back to a generic bad request response.
Response format:
{
"meta": {
"success": false,
"message": "First validation error message",
"errors": {
"Email": ["Email is required", "Email must be valid"],
"Password": ["Password must be at least 8 characters"]
}
},
"data": null
}
Parameters:
- c: *fiber.Ctx - The Fiber context
- err: error - The validation error (should be *validator.ValidationError)
Returns:
- error: Fiber error for response handling
Example:
if err := validator.ValidateStructWithContext(c, user); err != nil {
return response.ValidationErrorI18n(c, err)
}