From b5c6f8927cac4572f4a59e6c992ffaf765a25741 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Tue, 3 May 2022 16:50:28 +0200 Subject: [PATCH] Add exported function to create `HTTPError` This can be used by backends to influence the status code returned to clients for errors that occurred in the backend. --- server.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server.go b/server.go index 811ef10..bc11acd 100644 --- a/server.go +++ b/server.go @@ -40,6 +40,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { hh.ServeHTTP(w, r) } +// NewHTTPError creates a new error that is associated with an HTTP status code +// and optionally an error that lead to it. Backends can use this functions to +// return errors that convey some semantics (e.g. 404 not found, 403 access +// denied, etc) while also providing an (optional) arbitrary error context +// (intended for humans). +func NewHTTPError(statusCode int, cause error) error { + return &internal.HTTPError{Code: statusCode, Err: cause} +} + type backend struct { FileSystem FileSystem }