1
0

chore(web): use errors.As instead of type assertion

(cherry picked from commit 57ba073874c23772b2b80bc12be8578860e9c7ec)
This commit is contained in:
kolaente
2024-08-29 16:28:16 +02:00
parent 198b2e3b70
commit 5768648760
6 changed files with 16 additions and 6 deletions

View File

@ -17,6 +17,7 @@
package handler
import (
"errors"
"fmt"
"net/http"
@ -31,7 +32,8 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
// Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error())
if he, is := err.(*echo.HTTPError); is {
var he *echo.HTTPError
if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
}
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided."))

View File

@ -17,6 +17,7 @@
package handler
import (
"errors"
"fmt"
"net/http"
@ -36,7 +37,8 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
// Bind params to struct
if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error())
if he, is := err.(*echo.HTTPError); is {
var he *echo.HTTPError
if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
}
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided."))

View File

@ -17,6 +17,7 @@
package handler
import (
"errors"
"fmt"
"math"
"net/http"
@ -38,7 +39,8 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
// Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error())
if he, is := err.(*echo.HTTPError); is {
var he *echo.HTTPError
if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
}
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided."))

View File

@ -17,6 +17,7 @@
package handler
import (
"errors"
"fmt"
"net/http"
"strconv"
@ -32,7 +33,8 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
// Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error())
if he, is := err.(*echo.HTTPError); is {
var he *echo.HTTPError
if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
}
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided."))

View File

@ -17,6 +17,7 @@
package handler
import (
"errors"
"fmt"
"net/http"
@ -32,7 +33,8 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
// Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error())
if he, is := err.(*echo.HTTPError); is {
var he *echo.HTTPError
if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
}
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided."))