httputil

package
v0.78.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(statusCode int, err error) error

Error creates a new HTTP error with a status code from an existing error.

func Errorf

func Errorf(statusCode int, format string, args ...interface{}) error

Errorf creates a new HTTP error with a status code and formatted message message.

func WriteError

func WriteError(w http.ResponseWriter, err error)

WriteError writes an error to the response as a JSON object. If the error is an instance of Error, the status code will be set to the error's code. Otherwise, it will return a 400 status code.

Types

type HTTPError

type HTTPError struct {
	StatusCode int
	Err        error
}

HTTPError represents an error with a HTTP status code.

func (HTTPError) Error

func (e HTTPError) Error() string

func (HTTPError) Unwrap

func (e HTTPError) Unwrap() error

type Handler

type Handler func(http.ResponseWriter, *http.Request) error

Handler is similar to http.HandlerFunc with simple error handling built in. If an error is returned, it will be written to the response as a JSON object (using WriteError).

func (Handler) ServeHTTP

func (fn Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ResponseWriter added in v0.64.1

type ResponseWriter interface {
	http.ResponseWriter
	// Status returns the HTTP status of the request, or 0 if one has not
	// yet been sent.
	Status() int
	// BytesWritten returns the total number of bytes sent to the client.
	BytesWritten() int
}

ResponseWriter wraps an http.ResponseWriter with response metadata tracking.

func WrapResponseWriter added in v0.64.1

func WrapResponseWriter(w http.ResponseWriter, protoMajor int) ResponseWriter

WrapResponseWriter wraps an http.ResponseWriter with response metadata tracking.

Jump to

Keyboard shortcuts

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