feat(api): add additional error codes

This commit is contained in:
wpetit 2023-04-19 10:21:46 +02:00
parent a1f09bafcc
commit a94d9ed720
1 changed files with 7 additions and 0 deletions

View File

@ -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,