Documentation
¶
Index ¶
- func Handle(pattern string, mux *http.ServeMux, handler HandlerFunc)
- func HandleContext(pattern string, mux *http.ServeMux, handler ContextHandlerFunc)
- func HandleContextFunc(pattern string, handler ContextHandlerFunc)
- func HandleFunc(pattern string, handler HandlerFunc)
- type ContextHandler
- type ContextHandlerFunc
- type Formatter
- type FormatterFunc
- type HTTPError
- func AsHTTPError(err error) HTTPError
- func BadGateway(message string) HTTPError
- func BadRequest(message string) HTTPError
- func BadRequestf(format string, args ...interface{}) HTTPError
- func Conflict(message string) HTTPError
- func Forbidden(message string) HTTPError
- func GatewayTimeout(message string) HTTPError
- func InternalServerError(message string) HTTPError
- func InternalServerErrorf(format string, args ...interface{}) HTTPError
- func MethodNotAllowed(message string) HTTPError
- func New(code int, message string) HTTPError
- func NotFound(message string) HTTPError
- func NotImplemented(message string) HTTPError
- func ServiceUnavailable(message string) HTTPError
- func Unauthorized(message string) HTTPError
- func UnprocessableEntity(message string) HTTPError
- func WithHeaders(err HTTPError, headers map[string]string) HTTPError
- func Wrap(code int, message string, err error) HTTPError
- type Handler
- type HandlerFunc
- type PlainTextFormatter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handle ¶
func Handle(pattern string, mux *http.ServeMux, handler HandlerFunc)
Handle creates a new Handler and registers it with a ServeMux
func HandleContext ¶
func HandleContext(pattern string, mux *http.ServeMux, handler ContextHandlerFunc)
HandleContext creates a new ContextHandler and registers it with a ServeMux
func HandleContextFunc ¶
func HandleContextFunc(pattern string, handler ContextHandlerFunc)
HandleContextFunc creates a new ContextHandler and registers it with DefaultServeMux
func HandleFunc ¶
func HandleFunc(pattern string, handler HandlerFunc)
HandleFunc creates a new Handler and registers it with DefaultServeMux
Types ¶
type ContextHandler ¶
type ContextHandler struct {
// contains filtered or unexported fields
}
ContextHandler wraps a ContextHandlerFunc to implement http.Handler
func NewContextHandler ¶
func NewContextHandler(h ContextHandlerFunc) *ContextHandler
NewContextHandler creates a new ContextHandler with default formatter
func NewContextHandlerWithFormatter ¶
func NewContextHandlerWithFormatter(h ContextHandlerFunc, formatter Formatter) *ContextHandler
NewContextHandlerWithFormatter creates a new ContextHandler with custom formatter
func (*ContextHandler) ServeHTTP ¶
func (h *ContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler
type ContextHandlerFunc ¶
ContextHandlerFunc is a handler that receives context as first parameter
type Formatter ¶
type Formatter interface {
Format(w http.ResponseWriter, r *http.Request, err HTTPError)
}
Formatter handles error formatting for different content types
type FormatterFunc ¶
type FormatterFunc func(w http.ResponseWriter, r *http.Request, err HTTPError)
FormatterFunc allows using a function as a Formatter
func (FormatterFunc) Format ¶
func (ff FormatterFunc) Format(w http.ResponseWriter, r *http.Request, err HTTPError)
Format implements the Formatter interface
type HTTPError ¶
HTTPError represents an HTTP error with status code and message
func AsHTTPError ¶
AsHTTPError converts a regular error to HTTPError, defaulting to 500 if not already an HTTPError
func BadGateway ¶
BadGateway creates a 502 Bad Gateway error
func BadRequest ¶
BadRequest creates a 400 Bad Request error
func BadRequestf ¶
BadRequestf creates a 400 Bad Request error with formatting
func GatewayTimeout ¶
GatewayTimeout creates a 504 Gateway Timeout error
func InternalServerError ¶
InternalServerError creates a 500 Internal Server Error
func InternalServerErrorf ¶
InternalServerErrorf creates a 500 Internal Server Error with formatting
func MethodNotAllowed ¶
MethodNotAllowed creates a 405 Method Not Allowed error
func NotImplemented ¶
NotImplemented creates a 501 Not Implemented error
func ServiceUnavailable ¶
ServiceUnavailable creates a 503 Service Unavailable error
func Unauthorized ¶
Unauthorized creates a 401 Unauthorized error
func UnprocessableEntity ¶
UnprocessableEntity creates a 422 Unprocessable Entity error
func WithHeaders ¶
WithHeaders adds headers to an HTTPError
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler wraps a HandlerFunc to implement http.Handler
func NewHandler ¶
func NewHandler(h HandlerFunc) *Handler
NewHandler creates a new Handler with default formatter
func NewHandlerWithFormatter ¶
func NewHandlerWithFormatter(h HandlerFunc, formatter Formatter) *Handler
NewHandlerWithFormatter creates a new Handler with custom formatter
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
HandlerFunc is a function that returns an HTTPError instead of writing directly to ResponseWriter
type PlainTextFormatter ¶
type PlainTextFormatter struct{}
PlainTextFormatter is a simple formatter that returns plain text error messages
func (*PlainTextFormatter) Format ¶
func (f *PlainTextFormatter) Format(w http.ResponseWriter, r *http.Request, err HTTPError)
Format implements Formatter interface for plain text responses