Documentation
¶
Overview ¶
Package apiframework provides HTTP request/response helpers used by the runtime API.
Handlers should use Encode, Decode, Error, GetPathParam, and GetQueryParam for JSON I/O and errors. For OpenAPI generation (tools/openapi-gen), place // @request pkg.Type after Decode and // @response pkg.Type after Encode; path and query documentation uses the description arguments on GetPathParam / GetQueryParam. Keep those helper calls in the route handler body so the static generator can discover them directly.
Index ¶
- Variables
- func Decode[T any](r *http.Request) (T, error)
- func Encode[T any](w http.ResponseWriter, _ *http.Request, status int, v T) error
- func EnforceToken(token string, next http.Handler) http.Handler
- func Error(w http.ResponseWriter, r *http.Request, err error, op Operation) error
- func GetErrorParam(err error) string
- func GetPathParam(r *http.Request, name string, description string) string
- func GetQueryParam(r *http.Request, name, defaultValue, description string) string
- func GetVersion() string
- func HandleAPIError(resp *http.Response) error
- func IsAPIError(err error) (message, errorCode, errorType, param string, ok bool)
- func RequestIDMiddleware(next http.Handler) http.Handler
- func TokenMiddleware(next http.Handler) http.Handler
- func TracingMiddleware(next http.Handler) http.Handler
- type APIError
- func BadPathValue(param string, message ...string) *APIError
- func BadRequest(message ...string) *APIError
- func Conflict(message ...string) *APIError
- func FileEmpty(message ...string) *APIError
- func FileSizeLimitExceeded(message ...string) *APIError
- func Forbidden(message ...string) *APIError
- func ImmutableGroup(message ...string) *APIError
- func ImmutableModel(message ...string) *APIError
- func InternalServerError(message ...string) *APIError
- func InvalidChain(message ...string) *APIError
- func InvalidParameterValue(param string, message ...string) *APIError
- func MissingParameter(param string, message ...string) *APIError
- func NewAPIError(err error, message, param string) *APIError
- func NotFound(message ...string) *APIError
- func Unauthorized(message ...string) *APIError
- func UnprocessableEntity(message ...string) *APIError
- func UnsupportedMediaType(message ...string) *APIError
- type AboutServer
- type ContextKey
- type MessageResponse
- type Operation
Constants ¶
This section is empty.
Variables ¶
var ( ErrEncodeInvalidJSON = errors.New("serverops: encoding failing, invalid json") ErrDecodeInvalidJSON = errors.New("serverops: decoding failing, invalid json") ErrDecodeInvalidYAML = errors.New("serverops: decoding failing, invalid yaml") ErrDecodeBase64 = errors.New("serverops: decoding failing, invalid base64 data") ErrUnsupportedContentType = errors.New("serverops: unsupported content type for decoding") ErrReadingRequestBody = errors.New("serverops: failed to read request body") ErrMalformedContentType = errors.New("serverops: malformed Content-Type header") )
var ( ErrInvalidParameterValue = errors.New("serverops: invalid parameter value type") ErrBadPathValue = errors.New("serverops: bad path value") ErrBadQueryValue = errors.New("serverops: bad query value") ErrImmutableModel = errors.New("serverops: immutable model") ErrImmutableGroup = errors.New("serverops: immutable group") ErrMissingParameter = errors.New("serverops: missing parameter") ErrEmptyRequest = errors.New("serverops: empty request") ErrEmptyRequestBody = errors.New("serverops: empty request body") ErrInvalidChain = errors.New("serverops: invalid chain definition") // The generic error types for common HTTP status codes ErrBadRequest = errors.New("serverops: bad request") ErrUnprocessableEntity = errors.New("serverops: unprocessable entity") ErrNotFound = errors.New("serverops: not found") ErrConflict = errors.New("serverops: conflict") ErrForbidden = errors.New("serverops: forbidden") ErrInternalServerError = errors.New("serverops: internal server error") ErrUnsupportedMediaType = errors.New("serverops: unsupported media type") ErrFileSizeLimitExceeded = errors.New("serverops: file size limit exceeded") ErrFileEmpty = errors.New("serverops: file cannot be empty") )
Standard error constants
Functions ¶
func GetErrorParam ¶
GetErrorParam extracts parameter from error if available.
func GetPathParam ¶
GetPathParam retrieves a URL path parameter by name and is used to enforce that all path parameters are documented for the OpenAPI generator. The description argument is not used at runtime but is required for API spec generation.
func GetQueryParam ¶
GetQueryParam retrieves a URL query parameter by name. If the parameter is not present, it returns the provided defaultValue. The description is used for OpenAPI generation.
func GetVersion ¶
func GetVersion() string
func HandleAPIError ¶
HandleAPIError processes error responses from the API
func IsAPIError ¶
IsAPIError checks if an error is an APIError and returns its components.
Types ¶
type APIError ¶
type APIError struct {
// contains filtered or unexported fields
}
APIError wraps an error with parameter context for API responses.
func BadPathValue ¶
func BadRequest ¶
func FileSizeLimitExceeded ¶
func ImmutableGroup ¶
func ImmutableModel ¶
func InternalServerError ¶
func InvalidChain ¶
func InvalidParameterValue ¶
Specific Error Constructors
func MissingParameter ¶
func NewAPIError ¶
NewAPIError creates a generic APIError with context. If message is empty, it falls back to the underlying error's message.
func Unauthorized ¶
func UnprocessableEntity ¶
func UnsupportedMediaType ¶
type AboutServer ¶
type MessageResponse ¶ added in v0.6.6
type MessageResponse struct {
Message string `json:"message"`
}
MessageResponse is the preferred success body for simple endpoints that only need to return a human-readable message without introducing a route-specific DTO shape.
Route handlers should still use Encode and annotate the call with:
// @response apiframework.MessageResponse