From a94d9ed7202b5eb165013fa737339c0bf8327467 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Apr 2023 10:21:46 +0200 Subject: [PATCH] feat(api): add additional error codes --- api/bind.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/bind.go b/api/bind.go index 520aaf7..a9b6d71 100644 --- a/api/bind.go +++ b/api/bind.go @@ -4,6 +4,8 @@ import ( "encoding/json" "net/http" + "github.com/pkg/errors" + "gitlab.com/wpetit/goweb/logger" validator "gopkg.in/go-playground/validator.v9" ) @@ -11,6 +13,10 @@ const ( ErrCodeMalformedRequest ErrorCode = "malformed-request" ErrCodeInvalidRequest ErrorCode = "invalid-request" ErrCodeInvalidFieldValue ErrorCode = "invalid-field-value" + ErrCodeNotFound ErrorCode = "not-found" + ErrCodeUnknownError ErrorCode = "unknown-error" + ErrCodeUnauthorized ErrorCode = "unauthorized" + ErrCodeForbidden ErrorCode = "forbidden" ) // Bind parses and bind the request body to the given target @@ -21,6 +27,7 @@ const ( // validation. func Bind(w http.ResponseWriter, r *http.Request, target interface{}) (ok bool) { if err := parseBody(r, target); err != nil { + logger.Error(r.Context(), "malformed request", logger.E(errors.WithStack(err))) ErrorResponse( w, http.StatusBadRequest, ErrCodeMalformedRequest,