Better error formatting

This commit is contained in:
rxdn 2023-04-13 17:47:59 +01:00
parent e5148d4086
commit 25f397c22d
5 changed files with 44 additions and 31 deletions

View File

@ -11,7 +11,6 @@ import (
"github.com/rxdn/gdl/objects/interaction/component"
"sort"
"strconv"
"strings"
)
type (
@ -59,12 +58,7 @@ func UpdateInputs(ctx *gin.Context) {
return
}
formatted := "Your input contained the following errors:"
for _, validationError := range validationErrors {
formatted += fmt.Sprintf("\n%s", validationError.Error())
}
formatted = strings.TrimSuffix(formatted, "\n")
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
ctx.JSON(400, utils.ErrorStr(formatted))
return
}

View File

@ -1,13 +1,11 @@
package api
import (
"fmt"
dbclient "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/database"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"strings"
)
type integrationCreateBody struct {
@ -64,12 +62,7 @@ func CreateIntegrationHandler(ctx *gin.Context) {
return
}
formatted := "Your input contained the following errors:"
for _, validationError := range validationErrors {
formatted += fmt.Sprintf("\n%s", validationError.Error())
}
formatted = strings.TrimSuffix(formatted, "\n")
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
ctx.JSON(400, utils.ErrorStr(formatted))
return
}

View File

@ -2,7 +2,6 @@ package api
import (
"errors"
"fmt"
"github.com/TicketsBot/GoPanel/botcontext"
dbclient "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/rpc"
@ -234,20 +233,14 @@ var urlRegex = regexp.MustCompile(`^https?://([-a-zA-Z0-9@:%._+~#=]{1,256})\.[a-
var validate = validator.New()
func (p *panelBody) doValidations(ctx *gin.Context, guildId uint64) bool {
err := validate.Struct(p)
if err != nil {
if err := validate.Struct(p); err != nil {
validationErrors, ok := err.(validator.ValidationErrors)
if !ok {
ctx.JSON(500, utils.ErrorStr("An error occurred while validating the panel"))
return false
}
formatted := "Your input contained the following errors:"
for _, validationError := range validationErrors {
formatted += fmt.Sprintf("\n%s", validationError.Error())
}
formatted = strings.TrimSuffix(formatted, "\n")
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
ctx.JSON(400, utils.ErrorStr(formatted))
return false
}

View File

@ -1,7 +1,6 @@
package api
import (
"fmt"
dbclient "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/GoPanel/utils/types"
@ -58,12 +57,7 @@ func CreateTag(ctx *gin.Context) {
return
}
formatted := "Your input contained the following errors:"
for _, validationError := range validationErrors {
formatted += fmt.Sprintf("\n%s", validationError.Error())
}
formatted = strings.TrimSuffix(formatted, "\n")
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
ctx.JSON(400, utils.ErrorStr(formatted))
return
}

39
utils/validationutils.go Normal file
View File

@ -0,0 +1,39 @@
package utils
import (
"fmt"
"github.com/go-playground/validator/v10"
"reflect"
"strings"
)
func FormatValidationError(err validator.FieldError) string {
switch err.Tag() {
case "max":
if err.Type() == reflect.TypeOf("") {
return fmt.Sprintf("Field \"%s\" cannot exceed %s characters in length", err.Field(), err.Param())
} else {
return fmt.Sprintf("Field \"%s\" cannot be greater than %s", err.Field(), err.Param())
}
case "min":
if err.Type() == reflect.TypeOf("") {
return fmt.Sprintf("Field \"%s\" must be at least %s characters in length", err.Field(), err.Param())
} else {
return fmt.Sprintf("Field \"%s\" cannot be less than %s", err.Field(), err.Param())
}
case "required":
return fmt.Sprintf("Field \"%s\" is required", err.Field())
default:
return err.Error()
}
}
func FormatValidationErrors(errors validator.ValidationErrors) string {
var formatted string
for _, err := range errors {
formatted += FormatValidationError(err) + "\n"
}
formatted = strings.TrimSuffix(formatted, "\n")
return formatted
}