Documentation
¶
Index ¶
- Variables
- func Created(w interface{ ... }, location string, data any)
- func Error(w JSONWriter, err error, statusForCode HTTPStatusMapper)
- func ErrorWithCode(w JSONWriter, statusForCode HTTPStatusMapper, code v1.ErrorCode, ...)
- func LegacyHTTPStatusForErrorCode(code v1.ErrorCode) (int, bool)
- func NoContent(w interface{ ... })
- func Success(w JSONWriter, data any)
- type APIError
- type HTTPStatusMapper
- type JSONWriter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidRequest represents a bad request error. ErrInvalidRequest = &APIError{ Code: v1.ErrorCodeInvalidRequest, Message: "invalid request", } // ErrRouteNotFound indicates that the request path has no API route. ErrRouteNotFound = &APIError{ Code: v1.ErrorCodeRouteNotFound, Message: "route not found", } // ErrMethodNotAllowed indicates that the route rejects the HTTP method. ErrMethodNotAllowed = &APIError{ Code: v1.ErrorCodeMethodNotAllowed, Message: "method not allowed", } ErrUnauthorized = &APIError{ Code: v1.ErrorCodeUnauthorized, Message: "unauthorized", } // ErrForbidden represents a permission denied error. ErrForbidden = &APIError{ Code: v1.ErrorCodeForbidden, Message: "permission denied", } // ErrNotFound represents a resource not found error. ErrNotFound = &APIError{ Code: v1.ErrorCodeNotFound, Message: "not found", } // ErrConflict represents a conflict error (e.g., resource already exists). ErrConflict = &APIError{ Code: v1.ErrorCodeConflict, Message: "conflict", } // ErrInternal represents an internal server error. ErrInternal = &APIError{ Code: v1.ErrorCodeInternal, Message: "internal error", } // ErrRequestTooLarge indicates that the request body exceeds the limit. ErrRequestTooLarge = &APIError{ Code: v1.ErrorCodeRequestTooLarge, Message: "request body is too large", } // ErrUnsupportedMediaType indicates an unsupported request content type. ErrUnsupportedMediaType = &APIError{ Code: v1.ErrorCodeUnsupportedMediaType, Message: "request content type is not supported", } // ErrTooManyRequests represents a rate limit exceeded error. ErrTooManyRequests = &APIError{ Code: v1.ErrorCodeRateLimited, Message: "too many requests", } )
Predefined errors
Functions ¶
func Created ¶
func Created(w interface {
JSONWriter
Header(key, val string)
}, location string, data any,
)
Created sends a 201 Created response with a Location header pointing at the new resource.
func Error ¶
func Error(w JSONWriter, err error, statusForCode HTTPStatusMapper)
Error sends an error response. If err exposes an API code, the generated directory determines its status. Otherwise, it returns HTTP 500 Internal Server Error.
func ErrorWithCode ¶
func ErrorWithCode( w JSONWriter, statusForCode HTTPStatusMapper, code v1.ErrorCode, message string, )
ErrorWithCode sends an API error using the status assigned to its code.
func LegacyHTTPStatusForErrorCode ¶
LegacyHTTPStatusForErrorCode resolves shared codes and compatibility codes.
func NoContent ¶
func NoContent(w interface{ Status(code int) })
NoContent sends a 204 No Content response with no body.
func Success ¶
func Success(w JSONWriter, data any)
Success sends a successful response with HTTP 200 status code.
Types ¶
type APIError ¶
APIError represents a standardized API error.
func NewAPIError ¶
NewAPIError creates an APIError whose status is derived from its code.
func (*APIError) GetMessage ¶
GetMessage returns the error message.
func (*APIError) WithMessage ¶
WithMessage returns a copy of the error with a custom message.
type HTTPStatusMapper ¶
HTTPStatusMapper resolves an API error code to its request-level status.
func ChainHTTPStatusMappers ¶
func ChainHTTPStatusMappers(mappers ...HTTPStatusMapper) HTTPStatusMapper
ChainHTTPStatusMappers returns the first successful mapper result.
type JSONWriter ¶
JSONWriter is the minimal interface required for writing JSON responses. *httpserver.Context implements this interface.