response

package
v0.0.0-...-b125894 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 represents an authentication error.
	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

func LegacyHTTPStatusForErrorCode(code v1.ErrorCode) (int, bool)

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

type APIError struct {
	Code    v1.ErrorCode
	Message string
}

APIError represents a standardized API error.

func NewAPIError

func NewAPIError(code v1.ErrorCode, message string) *APIError

NewAPIError creates an APIError whose status is derived from its code.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

func (*APIError) GetCode

func (e *APIError) GetCode() v1.ErrorCode

GetCode returns the application error code.

func (*APIError) GetMessage

func (e *APIError) GetMessage() string

GetMessage returns the error message.

func (*APIError) WithMessage

func (e *APIError) WithMessage(message string) *APIError

WithMessage returns a copy of the error with a custom message.

type HTTPStatusMapper

type HTTPStatusMapper func(code v1.ErrorCode) (int, bool)

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

type JSONWriter interface {
	JSON(code int, obj any)
}

JSONWriter is the minimal interface required for writing JSON responses. *httpserver.Context implements this interface.

Jump to

Keyboard shortcuts

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