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" "encoding/json"
"net/http" "net/http"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
validator "gopkg.in/go-playground/validator.v9" validator "gopkg.in/go-playground/validator.v9"
) )
@ -11,6 +13,10 @@ const (
ErrCodeMalformedRequest ErrorCode = "malformed-request" ErrCodeMalformedRequest ErrorCode = "malformed-request"
ErrCodeInvalidRequest ErrorCode = "invalid-request" ErrCodeInvalidRequest ErrorCode = "invalid-request"
ErrCodeInvalidFieldValue ErrorCode = "invalid-field-value" 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 // Bind parses and bind the request body to the given target
@ -21,6 +27,7 @@ const (
// validation. // validation.
func Bind(w http.ResponseWriter, r *http.Request, target interface{}) (ok bool) { func Bind(w http.ResponseWriter, r *http.Request, target interface{}) (ok bool) {
if err := parseBody(r, target); err != nil { if err := parseBody(r, target); err != nil {
logger.Error(r.Context(), "malformed request", logger.E(errors.WithStack(err)))
ErrorResponse( ErrorResponse(
w, http.StatusBadRequest, w, http.StatusBadRequest,
ErrCodeMalformedRequest, ErrCodeMalformedRequest,