apierrors

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package apierrors provides structured API error codes and responses. All codes are namespaced (e.g., "core:unauthorized", "stats:export_failed").

Index

Constants

View Source
const (
	// Authentication & Authorization
	CodeUnauthorized = "core:unauthorized"
	CodeForbidden    = "core:forbidden"
	CodeInvalidToken = "core:invalid_token"
	CodeTokenExpired = "core:token_expired"
	CodeTokenRevoked = "core:token_revoked"

	// Request errors
	CodeInvalidRequest    = "core:invalid_request"
	CodeValidationFailed  = "core:validation_failed"
	CodeInvalidScope      = "core:invalid_scope"
	CodeInvalidExpiration = "core:invalid_expiration"
	CodeInvalidID         = "core:invalid_id"

	// Resource errors
	CodeNotFound      = "core:not_found"
	CodeTokenNotFound = "core:token_not_found"
	CodeConflict      = "core:conflict"

	// Rate limiting
	CodeRateLimited = "core:rate_limited"

	// Server errors
	CodeInternalError      = "core:internal_error"
	CodeServiceUnavailable = "core:service_unavailable"
)

Core error codes - registered automatically at init

Variables

View Source
var Registry = &registry{
	codes: make(map[string]ErrorCode),
	byNS:  make(map[string][]string),
}

Registry is the global error code registry

Functions

func Error

func Error(c *gin.Context, code string)

Error sends an error response using a registered error code It looks up the code in the registry for HTTP status and default message

func ErrorWithMessage

func ErrorWithMessage(c *gin.Context, code, message string)

ErrorWithMessage sends an error response with a custom message Useful when the message needs dynamic content (e.g., validation details)

func ErrorWithStatus

func ErrorWithStatus(c *gin.Context, status int, code, message string)

ErrorWithStatus sends an error response with a custom HTTP status Use when the registered status isn't appropriate for the context

Types

type APIError

type APIError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

APIError represents the JSON error response structure

func New

func New(code string) APIError

New creates an APIError without sending a response Useful for building error responses manually

func NewWithMessage

func NewWithMessage(code, message string) APIError

NewWithMessage creates an APIError with a custom message

type ErrorCode

type ErrorCode struct {
	Code       string `json:"code"`        // Full namespaced code (e.g., "core:not_found")
	Message    string `json:"message"`     // Default English message
	HTTPStatus int    `json:"http_status"` // Suggested HTTP status code
}

ErrorCode represents a registered API error code

type ErrorEnumerator

type ErrorEnumerator interface {
	EnumerateErrors() []ErrorCode
}

ErrorEnumerator is implemented by plugins to declare their error codes

Jump to

Keyboard shortcuts

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