41 lines
742 B
Go
41 lines
742 B
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
|
|
rErrors "codeberg.org/danjones000/responsable-errors"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ErrorMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Next()
|
|
err := c.Errors.Last()
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
var re rErrors.ResponsableError
|
|
errors.As(err, &re)
|
|
for _, err = range c.Errors {
|
|
errors.As(err, &re)
|
|
if re != nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
// @todo we need to add some way to do custom handling
|
|
|
|
// @todo Refactor this with 👆
|
|
if re == nil {
|
|
switch err.Type {
|
|
case gin.ErrorTypePrivate:
|
|
re = rErrors.NewInternalError("%w", err)
|
|
default:
|
|
re = rErrors.NewBadRequest("%w", err)
|
|
}
|
|
}
|
|
|
|
c.JSON(re.GetStatus(), gin.H{"error": re.GetMsg()})
|
|
}
|
|
}
|