responder

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(c *fiber.Ctx, err error) error

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

func NewAPIError(code ErrorCode) *APIError

NewAPIError creates an APIError based on the ErrorCode, defaulting to a generic error if not mapped

func (*APIError) Error

func (e *APIError) Error() string

Error returns the error message of the APIError.

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"
)

Jump to

Keyboard shortcuts

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