mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package httputils
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/foomo/keel/log"
|
|
keelhttp "github.com/foomo/keel/net/http"
|
|
)
|
|
|
|
// InternalServerError http response
|
|
func InternalServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, err error) {
|
|
ServerError(l, w, r, http.StatusInternalServerError, err)
|
|
}
|
|
|
|
// UnauthorizedServerError http response
|
|
func UnauthorizedServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, err error) {
|
|
ServerError(l, w, r, http.StatusUnauthorized, err)
|
|
}
|
|
|
|
// BadRequestServerError http response
|
|
func BadRequestServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, err error) {
|
|
ServerError(l, w, r, http.StatusBadRequest, err)
|
|
}
|
|
|
|
// NotFoundServerError http response
|
|
func NotFoundServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, err error) {
|
|
ServerError(l, w, r, http.StatusNotFound, err)
|
|
}
|
|
|
|
// ServerError http response
|
|
func ServerError(l *zap.Logger, w http.ResponseWriter, r *http.Request, code int, err error) {
|
|
if err != nil {
|
|
log.WithHTTPRequest(l, r).Error("http server error", log.FError(err), log.FHTTPStatusCode(code))
|
|
w.Header().Set(keelhttp.HeaderXError, err.Error())
|
|
http.Error(w, http.StatusText(code), code)
|
|
}
|
|
}
|