exceptions

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package exceptions holds the exception types an HTTP response can carry: HttpResponseException, MalformedUrlException, OriginMismatchException, PostTooLargeException and ThrottleRequestsException.

Constructors, and the shared struct that stands in for a hierarchy

Each type has a NewX function rather than a constructor: Go has no constructors, and the zero value of these types would be a 0 status, which is not an answer.

PostTooLargeException, ThrottleRequestsException and MalformedUrlException all carry an HTTP status the way a common base class would. Go has no class inheritance, so HTTPError is a struct they embed instead, and errors.As reaches it through any of them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPError

type HTTPError struct {
	// StatusCode is the status the exception answers with.
	StatusCode int
	// Message is the sentence shown with it.
	Message string
	// Headers are the response headers the exception carries.
	Headers stdhttp.Header
	// Code is an application-defined error code, independent of StatusCode.
	Code int
	// Previous is the error this one wraps.
	Previous error
}

HTTPError carries a status code, a message and headers common to several of this package's exceptions.

Go has no class inheritance, so it is a struct that MalformedUrlException, PostTooLargeException and ThrottleRequestsException each embed, and errors.As finds it in any of them.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error implements error.

func (*HTTPError) GetHeaders

func (e *HTTPError) GetHeaders() stdhttp.Header

GetHeaders returns Headers.

func (*HTTPError) GetStatusCode

func (e *HTTPError) GetStatusCode() int

GetStatusCode returns StatusCode.

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

Unwrap returns the wrapped error, so errors.Is and errors.As reach Previous.

type HttpResponseException

type HttpResponseException struct {
	// contains filtered or unexported fields
}

HttpResponseException is an error carrying a response that was built already, so the layer that catches it sends that response instead of rendering one.

func NewHttpResponseException

func NewHttpResponseException(response Response, previous ...error) *HttpResponseException

NewHttpResponseException builds an HttpResponseException. The variadic argument is an optional wrapped error.

func (*HttpResponseException) Error

func (e *HttpResponseException) Error() string

Error implements error, taking its message from the wrapped error when there is one, and a generic message when there is not.

func (*HttpResponseException) GetResponse

func (e *HttpResponseException) GetResponse() Response

GetResponse is the response to send.

func (*HttpResponseException) Unwrap

func (e *HttpResponseException) Unwrap() error

Unwrap returns the wrapped error.

type MalformedUrlException

type MalformedUrlException struct{ HTTPError }

MalformedUrlException is the 400 for a request whose path cannot be decoded: broken percent-encoding, or bytes that do not form valid UTF-8. No router can match such a path and no handler can read it, so the request is refused before either gets the chance.

It takes no arguments and carries no detail: the message is always "Malformed URL.", and the path that caused it is not repeated back to the caller.

func NewMalformedUrlException

func NewMalformedUrlException() *MalformedUrlException

NewMalformedUrlException builds a MalformedUrlException. It takes no arguments: there is nothing to configure.

type OriginMismatchException

type OriginMismatchException struct {
	// Message is the sentence. Empty uses the default in Error.
	Message string
}

OriginMismatchException is returned when a redirect target was not on the origin the request arrived on.

func NewOriginMismatchException

func NewOriginMismatchException(message string) *OriginMismatchException

NewOriginMismatchException builds an OriginMismatchException.

func (*OriginMismatchException) Error

func (e *OriginMismatchException) Error() string

Error implements error.

type PostTooLargeException

type PostTooLargeException struct{ HTTPError }

PostTooLargeException is the 413 for a request body bigger than the server agreed to accept. It is raised from the declared content length, before the body is read, so the caller is told the size was refused instead of being handed a parse failure on a body that was cut short.

func NewPostTooLargeException

func NewPostTooLargeException(message string, previous error, headers stdhttp.Header, code int) *PostTooLargeException

NewPostTooLargeException builds a PostTooLargeException.

type Response

type Response interface {
	// Status is the status code to send.
	Status() int
	// Content is the body to send.
	Content() string
}

Response is the minimal interface HttpResponseException needs to carry a response: enough to send it, and no more.

It is an interface and not the concrete hesape/http.Response for one reason: hesape/http returns these, so naming its type here would be a cycle. The two methods are what a handler that catches one reaches for -- the status to send and the body to write.

type ThrottleRequestsException

type ThrottleRequestsException struct{ HTTPError }

ThrottleRequestsException is the 429 for a caller that has spent its rate limit. A rate limiter raises it instead of running the handler, and puts the numbers it just computed into the headers -- how many attempts were allowed, how many are left, and how long until the next one is.

Those headers are the useful part: HTTPError.GetHeaders is where the layer that turns this into a response reads them, and a client that reads them in turn can wait rather than keep asking.

func NewThrottleRequestsException

func NewThrottleRequestsException(message string, previous error, headers stdhttp.Header, code int) *ThrottleRequestsException

NewThrottleRequestsException builds a ThrottleRequestsException.

Jump to

Keyboard shortcuts

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