trweb

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NotFound         = &HTTPError{Status: 404, Code: "not_found", Message: "Not Found"}
	Unauthorized     = &HTTPError{Status: 401, Code: "unauthorized", Message: "Unauthorized"}
	InternalError    = &HTTPError{Status: 500, Code: "internal_error", Message: "Internal Server Error"}
	MethodNotAllowed = &HTTPError{Status: 405, Code: "method_not_allowed", Message: "Method Not Allowed"}
	InvalidRequest   = &HTTPError{Status: 400, Code: "invalid_request", Message: "Invalid Request"}
)

Functions

func Decode

func Decode(r *http.Request, v any) error

Decode decodes the request body as HuJSON. If an error occurs it might wrap the error as an HTTPError.

func DecodeStrict

func DecodeStrict(r *http.Request, v any) error

StrictDecode works like Decode but does not allow unknown fields.

func Error

func Error(status int, code string, message string) error

func WriteError

func WriteError(w http.ResponseWriter, err error) bool

WriteError encodes err to w, setting the approriate headers, if the underlying type of err is an HTTPError and returns true; otherwise it does nothing and returns false.

Types

type HTTPError

type HTTPError struct {
	Source  string `json:"source,omitempty"`
	Status  int    `json:"status"`
	Code    string `json:"code"` // (e.g. "invalid_request")
	Message string `json:"message"`
}

func (*HTTPError) Error

func (e *HTTPError) Error() string

Jump to

Keyboard shortcuts

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