apierrors

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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" // #nosec G101 -- API error code identifier, not a credential
	CodeTokenRevoked = "core:token_revoked" // #nosec G101 -- API error code identifier, not a credential

	// 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" // #nosec G101 -- API error code identifier, not a credential
	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