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

Add more commonly used HTTP error codes #6

Merged
merged 1 commit into from
Feb 28, 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
50 changes: 40 additions & 10 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,71 @@ type Engine struct {
Writer
}

// BadRequest replies with an HTTP Status 400 Bad Request.
// BadRequest replies with HTTP Status 400 Bad Request.
func (e Engine) BadRequest(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}

// Unauthorized replies with an HTTP Status 401 Unauthorized.
// Unauthorized replies with HTTP Status 401 Unauthorized.
func (e Engine) Unauthorized(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}

// Forbidden replies with an HTTP Status 403 Forbidden.
// Forbidden replies with HTTP Status 403 Forbidden.
func (e Engine) Forbidden(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
}

// NotFound replies with an HTTP Status 404 Not Found.
// NotFound replies with HTTP Status 404 Not Found.
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 an HTTP Status 405 Method Not Allowed.
// 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, ", "))
e.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}

// NotAcceptable replies with HTTP Status 406 Not Acceptable.
func (e Engine) NotAcceptable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotAcceptable), http.StatusNotAcceptable)
}

// RequestTimeout replies with HTTP Status 408 Request Timeout.
func (e Engine) RequestTimeout(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestTimeout), http.StatusRequestTimeout)
}

// Conflict replies with HTTP Status 409 Conflict.
func (e Engine) Conflict(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
}

// Gone replies with HTTP Status 410 Gone.
func (e Engine) Gone(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusGone), http.StatusGone)
}

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

// 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 an HTTP Status 500 Internal Server Error.
// 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()))
e.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

// ReplyOrError wraps Reply with error debugging - if an error is encountered in
// 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.
func (e Engine) ReplyOrError(w http.ResponseWriter, code int, opts Options) {
Expand All @@ -73,17 +103,17 @@ func (e Engine) ReplyOrError(w http.ResponseWriter, code int, opts Options) {
}
}

// OK replies with an HTTP 200 Status OK.
// OK replies with HTTP 200 Status OK.
func (e Engine) OK(w http.ResponseWriter, opts Options) {
e.ReplyOrError(w, http.StatusOK, opts)
}

// Created replies with an HTTP 201 Status Created.
// Created replies with HTTP 201 Status Created.
func (e Engine) Created(w http.ResponseWriter, opts Options) {
e.ReplyOrError(w, http.StatusCreated, opts)
}

// NoContent replies with an HTTP Status 204 No Content.
// NoContent replies with HTTP Status 204 No Content.
func (e Engine) NoContent(w http.ResponseWriter) {
e.ReplyOrError(w, http.StatusNoContent, Options{Key: "no_content.html"})
}
Loading
Loading