Documentation
¶
Overview ¶
Package apierrors provides structured API error codes and responses. All codes are namespaced (e.g., "core:unauthorized", "stats:export_failed").
Index ¶
Constants ¶
const ( // Authentication & Authorization 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" )
Core error codes - registered automatically at init
Variables ¶
var Registry = ®istry{ codes: make(map[string]ErrorCode), byNS: make(map[string][]string), }
Registry is the global error code registry
Functions ¶
func Error ¶
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 ¶
ErrorWithMessage sends an error response with a custom message Useful when the message needs dynamic content (e.g., validation details)
Types ¶
type APIError ¶
APIError represents the JSON error response structure
func New ¶
New creates an APIError without sending a response Useful for building error responses manually
func NewWithMessage ¶
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