Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorHandler ¶
func WriteJSONResponse ¶
func WriteJSONResponse(c *fiber.Ctx, apiResponse APIResponse)
WriteJSONResponse writes the API response as JSON to the HTTP response writer.
Types ¶
type APIError ¶
type APIError struct { Code string `json:"code"` // Error code string (e.g., "auth_invalid_token"). Description string `json:"description"` // User-friendly description of the error. StatusCode int `json:"status_code"` // HTTP status code. }
APIError represents a structured error message.
func NewAPIError ¶
NewAPIError creates an APIError based on the ErrorCode, defaulting to a generic error if not mapped
type APIResponse ¶
type APIResponse struct { Type ResponseType `json:"type"` StatusCode int `json:"status_code"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` // Populated in case of success responses. Error *APIError `json:"error,omitempty"` // Populated in case of error responses. }
func CreateError ¶
func CreateError(code ErrorCode) APIResponse
CreateError generates an error response
func CreateSuccessResponse ¶
func CreateSuccessResponse(data interface{}, message string) APIResponse
CreateSuccessResponse generates a success response with data.
type ErrorCode ¶
type ErrorCode int
Define custom error and HTTP status codes
const ( // Authentication Errors ErrAuthInvalidToken ErrorCode = 40001 ErrAuthPermissionDenied ErrorCode = 40002 ErrAuthTokenExpired ErrorCode = 40003 ErrLimitReached ErrorCode = 40005 // Validation Errors ErrInvalidRequest ErrorCode = 40004 ErrValidationInvalidField ErrorCode = 40010 ErrValidationMissingField ErrorCode = 40011 ErrResourceNotFound ErrorCode = 40400 // Database Errors ErrDatabaseQueryFailed ErrorCode = 50001 ErrDatabaseConnection ErrorCode = 50002 ErrDatabaseRecordNotFound ErrorCode = 50003 ErrScannerFailed ErrorCode = 50004 // Access & Generic Errors ErrForbiddenAccess ErrorCode = 40300 ErrInternalServerError ErrorCode = 50000 ErrBadRequest ErrorCode = 40000 )
type ResponseType ¶
type ResponseType string
const ( ResponseTypeSuccess ResponseType = "success" ResponseTypeError ResponseType = "error" )
Click to show internal directories.
Click to hide internal directories.