mock shop page: can delete listings

This commit is contained in:
2026-02-05 17:18:56 -07:00
parent e7f39d69b3
commit 0e36012e6d
6 changed files with 163 additions and 28 deletions
+36 -2
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"ruben/inventory2/internal/consts"
"github.com/gin-gonic/gin"
)
@@ -46,11 +47,23 @@ func HandleErrors(c *gin.Context) {
}
}
if !ok {
var err error
var (
err error
status int
statusFound bool
)
for _, e := range c.Errors {
err = errors.Join(err, e)
if !statusFound {
status, statusFound = mapErrorConstantsToStatus(e)
}
}
c.String(http.StatusInternalServerError, err.Error())
if !statusFound {
status = http.StatusInternalServerError
}
c.String(status, err.Error())
return
}
@@ -131,3 +144,24 @@ func GetStatusFromError(err error) int {
return status
}
func mapErrorConstantsToStatus(err error) (int, bool) {
for {
switch err {
case consts.ErrBadRequest:
return http.StatusBadRequest, true
case consts.ErrNotFound:
return http.StatusNotFound, true
case consts.ErrConflict:
return http.StatusConflict, true
default:
werr, ok := err.(interface {
Unwrap() error
})
if !ok {
return 0, false
}
err = werr.Unwrap()
}
}
}