api

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package api provides shared REST API response envelope and error types.

Index

Constants

View Source
const (
	CodeUnauthorized        = "UNAUTHORIZED"
	CodeForbidden           = "FORBIDDEN"
	CodeValidationError     = "VALIDATION_ERROR"
	CodeNotFound            = "NOT_FOUND"
	CodeConflict            = "CONFLICT"
	CodeRateLimitExceeded   = "RATE_LIMIT_EXCEEDED"
	CodeInternalError       = "INTERNAL_ERROR"
	CodeBadRequest          = "BAD_REQUEST"
	CodeUnprocessableEntity = "UNPROCESSABLE_ENTITY"
	CodeServiceUnavailable  = "SERVICE_UNAVAILABLE"
)

Error codes for the API error envelope.

Variables

This section is empty.

Functions

func RequestID

func RequestID(r *http.Request) string

RequestID returns the request ID from the request (X-Request-ID header) or a new UUID.

func RespondError

func RespondError(w http.ResponseWriter, r *http.Request, err *APIError)

RespondError writes the error envelope and sets status code. Uses X-Request-ID from r or generates one.

func RespondJSON

func RespondJSON(w http.ResponseWriter, r *http.Request, statusCode int, data interface{}, meta *Meta)

RespondJSON writes a success response with envelope: { "data": data, "meta": meta }.

func RespondNoContent

func RespondNoContent(w http.ResponseWriter)

RespondNoContent writes 204 with no body.

Types

type APIError

type APIError struct {
	StatusCode int           `json:"-"`
	Code       string        `json:"code"`
	Message    string        `json:"message"`
	Details    []ErrorDetail `json:"details,omitempty"`
}

APIError carries status code and error code for handlers.

func (*APIError) Error

func (e *APIError) Error() string

type ErrorDetail

type ErrorDetail struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

ErrorDetail is a field-level validation error.

type ErrorEnvelope

type ErrorEnvelope struct {
	Error ErrorPayload `json:"error"`
}

ErrorEnvelope is the standard error response shape: { "error": { "code", "message", "requestId", "details" } }.

type ErrorPayload

type ErrorPayload struct {
	Code      string        `json:"code"`
	Message   string        `json:"message"`
	RequestID string        `json:"requestId,omitempty"`
	Details   []ErrorDetail `json:"details,omitempty"`
}

ErrorPayload is the inner error object.

type Meta

type Meta struct {
	RequestID string `json:"requestId,omitempty"`
}

Meta holds optional response metadata (e.g. requestId, pagination).

type SuccessEnvelope

type SuccessEnvelope struct {
	Data interface{} `json:"data"`
	Meta *Meta       `json:"meta,omitempty"`
}

SuccessEnvelope is the standard success response shape: { "data": ..., "meta": ... }.

Jump to

Keyboard shortcuts

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