Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update errors #7

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 136 additions & 12 deletions engine.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package reply

import (
"fmt"
"log/slog"
"net/http"
"runtime/debug"
"strings"
)

// Writer is used by an Engine to construct replies to HTTP server requests.
Expand All @@ -22,6 +18,8 @@ type Engine struct {
// sent in responses. If debug is false, the error string will simply be the
// plain text representation of the error code.
Debug bool

// Writer is an interface used to construct replies to HTTP server requests.
Writer
}

Expand All @@ -45,10 +43,8 @@ func (e Engine) NotFound(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}

// MethodNotAllowed sets an Allow header with the given methods,
// then replies with HTTP Status 405 Method Not Allowed.
func (e Engine) MethodNotAllowed(w http.ResponseWriter, allow ...string) {
w.Header().Set("Allow", strings.Join(allow, ", "))
// MethodNotAllowed replies with HTTP Status 405 Method Not Allowed.
func (e Engine) MethodNotAllowed(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}

Expand All @@ -57,6 +53,11 @@ func (e Engine) NotAcceptable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotAcceptable), http.StatusNotAcceptable)
}

// ProxyAuthRequired replies with HTTP Status 407 Proxy Authentication Required.
func (e Engine) ProxyAuthRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusProxyAuthRequired), http.StatusProxyAuthRequired)
}

// RequestTimeout replies with HTTP Status 408 Request Timeout.
func (e Engine) RequestTimeout(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestTimeout), http.StatusRequestTimeout)
Expand All @@ -72,23 +73,146 @@ func (e Engine) Gone(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusGone), http.StatusGone)
}

// LengthRequired replies with HTTP Status 411 Length Required.
func (e Engine) LengthRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusLengthRequired), http.StatusLengthRequired)
}

// PreconditionFailed replies with HTTP Status 412 Precondition Failed.
func (e Engine) PreconditionFailed(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
}

// RequestEntityTooLarge replies with HTTP Status 413 Request Entity Too Large.
func (e Engine) RequestEntityTooLarge(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
}

// RequestURITooLong replies with HTTP Status 414 Request URI Too Long.
func (e Engine) RequestURITooLong(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestURITooLong), http.StatusRequestURITooLong)
}

// UnsupportedMediaType replies with HTTP Status 415 Unsupported Media Type.
func (e Engine) UnsupportedMediaType(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnsupportedMediaType), http.StatusUnsupportedMediaType)
}

// RequestedRangeNotSatisfiable replies with HTTP Status 416 Requested Range Not Satisfiable.
func (e Engine) RequestedRangeNotSatisfiable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestedRangeNotSatisfiable), http.StatusRequestedRangeNotSatisfiable)
}

// ExpectationFailed replies with HTTP Status 417 Expectation Failed.
func (e Engine) ExpectationFailed(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusExpectationFailed), http.StatusExpectationFailed)
}

// Teapot replies with HTTP Status 418 I'm a teapot.
func (e Engine) Teapot(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusTeapot), http.StatusTeapot)
}

// MisdirectedRequest replies with HTTP Status 421 Misdirected Request.
func (e Engine) MisdirectedRequest(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusMisdirectedRequest), http.StatusMisdirectedRequest)
}

// UnprocessableEntity replies with HTTP Status 422 Unprocessable Entity.
func (e Engine) UnprocessableEntity(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
}

// Locked replies with HTTP Status 423 Locked.
func (e Engine) Locked(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusLocked), http.StatusLocked)
}

// FailedDependency replies with HTTP Status 424 Failed Dependency.
func (e Engine) FailedDependency(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusFailedDependency), http.StatusFailedDependency)
}

// TooEarly replies with HTTP Status 425 Too Early.
func (e Engine) TooEarly(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusTooEarly), http.StatusTooEarly)
}

// UpgradeRequired replies with HTTP Status 426 Upgrade Required.
func (e Engine) UpgradeRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUpgradeRequired), http.StatusUpgradeRequired)
}

// PreconditionRequired replies with HTTP Status 428 Precondition Required.
func (e Engine) PreconditionRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusPreconditionRequired), http.StatusPreconditionRequired)
}

// TooManyRequests replies with HTTP Status 429 Too Many Requests.
func (e Engine) TooManyRequests(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
}

// InternalServerError uses slog to log an error and stack trace,
// then replies with HTTP Status 500 Internal Server Error.
func (e Engine) InternalServerError(w http.ResponseWriter, err error) {
slog.Error(fmt.Sprintf("%s\n%s", err.Error(), debug.Stack()))
// RequestHeaderFieldsTooLarge replies with HTTP Status 431 Request Header Fields Too Large.
func (e Engine) RequestHeaderFieldsTooLarge(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestHeaderFieldsTooLarge), http.StatusRequestHeaderFieldsTooLarge)
}

// UnavailableForLegalReasons replies with HTTP Status 451 Unavailable For Legal Reasons.
func (e Engine) UnavailableForLegalReasons(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnavailableForLegalReasons), http.StatusUnavailableForLegalReasons)
}

// InternalServerError replies with HTTP Status 500 Internal Server Error.
func (e Engine) InternalServerError(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

// NotImplemented replies with HTTP Status 501 Not Implemented.
func (e Engine) NotImplemented(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
}

// BadGateway replies with HTTP Status 502 Bad Gateway.
func (e Engine) BadGateway(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
}

// ServiceUnavailable replies with HTTP Status 503 Service Unavailable.
func (e Engine) ServiceUnavailable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
}

// GatewayTimeout replies with HTTP Status 504 Gateway Timeout.
func (e Engine) GatewayTimeout(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout)
}

// HTTPVersionNotSupported replies with HTTP Status 505 HTTP Version Not Supported.
func (e Engine) HTTPVersionNotSupported(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusHTTPVersionNotSupported), http.StatusHTTPVersionNotSupported)
}

// VariantAlsoNegotiates replies with HTTP Status 506 Variant Also Negotiates.
func (e Engine) VariantAlsoNegotiates(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusVariantAlsoNegotiates), http.StatusVariantAlsoNegotiates)
}

// InsufficientStorage replies with HTTP Status 507 Insufficient Storage.
func (e Engine) InsufficientStorage(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusInsufficientStorage), http.StatusInsufficientStorage)
}

// LoopDetected replies with HTTP Status 508 Loop Detected.
func (e Engine) LoopDetected(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusLoopDetected), http.StatusLoopDetected)
}

// NetworkAuthenticationRequired replies with HTTP Status 511 Network Authentication Required
func (e Engine) NetworkAuthenticationRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNetworkAuthenticationRequired), http.StatusNetworkAuthenticationRequired)
}

// ReplyOrError wraps Reply with error debugging. If an error is encountered in
// Reply, the Writer's Error function is triggered. Error essages are replaced
// with 'Internal Server Error' if e.Debug is false.
Expand Down
Loading
Loading