diff options
| author | Taras Madan <tarasmadan@google.com> | 2022-05-13 11:26:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-13 11:26:08 +0200 |
| commit | 107f6434d376c0f6f108e7b1dccfedea7f0fcfa0 (patch) | |
| tree | 929265cdb6b7781e366b0bbd803e4d0e646cc04d /dashboard/app/handler.go | |
| parent | 7ce5a022b5ce12e980774b17996140ebcddc0c53 (diff) | |
dashboard/app: return 4xx instead of 5xx for user requests
* dashboard/app: return 4xx instead of 5xx for user requests
5xx category signals the Internal Server Errors and require server developers attention.
4xx category means client side problem and doesn't require server developers attention.
Added tests.
Diffstat (limited to 'dashboard/app/handler.go')
| -rw-r--r-- | dashboard/app/handler.go | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/dashboard/app/handler.go b/dashboard/app/handler.go index 58f4ff339..2cb0d2739 100644 --- a/dashboard/app/handler.go +++ b/dashboard/app/handler.go @@ -7,6 +7,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "errors" "fmt" "net/http" "sort" @@ -52,14 +53,18 @@ func handleContext(fn contextHandler) http.Handler { http.Redirect(w, r, redir.Error(), http.StatusFound) return } + + status := http.StatusInternalServerError logf := log.Errorf - if _, dontlog := err.(ErrDontLog); dontlog { + var clientError *ErrClient + if errors.As(err, &clientError) { // We don't log these as errors because they can be provoked // by invalid user requests, so we don't wan't to pollute error log. logf = log.Warningf + status = clientError.HTTPStatus() } logf(c, "%v", err) - w.WriteHeader(http.StatusInternalServerError) + w.WriteHeader(status) if err1 := templates.ExecuteTemplate(w, "error.html", data); err1 != nil { combinedError := fmt.Sprintf("got err \"%v\" processing ExecuteTemplate() for err \"%v\"", err1, err) http.Error(w, combinedError, http.StatusInternalServerError) @@ -69,10 +74,23 @@ func handleContext(fn contextHandler) http.Handler { } type ( - ErrDontLog struct{ error } + ErrClient struct{ error } ErrRedirect struct{ error } ) +var ErrClientNotFound = &ErrClient{errors.New("resource not found")} +var ErrClientBadRequest = &ErrClient{errors.New("bad request")} + +func (ce *ErrClient) HTTPStatus() int { + switch ce { + case ErrClientNotFound: + return http.StatusNotFound + case ErrClientBadRequest: + return http.StatusBadRequest + } + return http.StatusInternalServerError +} + func handleAuth(fn contextHandler) contextHandler { return func(c context.Context, w http.ResponseWriter, r *http.Request) error { if err := checkAccessLevel(c, r, config.AccessLevel); err != nil { |
