Better error formatting
This commit is contained in:
parent
e5148d4086
commit
25f397c22d
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/rxdn/gdl/objects/interaction/component"
|
"github.com/rxdn/gdl/objects/interaction/component"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -59,12 +58,7 @@ func UpdateInputs(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted := "Your input contained the following errors:"
|
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
|
||||||
for _, validationError := range validationErrors {
|
|
||||||
formatted += fmt.Sprintf("\n%s", validationError.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
formatted = strings.TrimSuffix(formatted, "\n")
|
|
||||||
ctx.JSON(400, utils.ErrorStr(formatted))
|
ctx.JSON(400, utils.ErrorStr(formatted))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
dbclient "github.com/TicketsBot/GoPanel/database"
|
dbclient "github.com/TicketsBot/GoPanel/database"
|
||||||
"github.com/TicketsBot/GoPanel/utils"
|
"github.com/TicketsBot/GoPanel/utils"
|
||||||
"github.com/TicketsBot/database"
|
"github.com/TicketsBot/database"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type integrationCreateBody struct {
|
type integrationCreateBody struct {
|
||||||
@ -64,12 +62,7 @@ func CreateIntegrationHandler(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted := "Your input contained the following errors:"
|
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
|
||||||
for _, validationError := range validationErrors {
|
|
||||||
formatted += fmt.Sprintf("\n%s", validationError.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
formatted = strings.TrimSuffix(formatted, "\n")
|
|
||||||
ctx.JSON(400, utils.ErrorStr(formatted))
|
ctx.JSON(400, utils.ErrorStr(formatted))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"github.com/TicketsBot/GoPanel/botcontext"
|
"github.com/TicketsBot/GoPanel/botcontext"
|
||||||
dbclient "github.com/TicketsBot/GoPanel/database"
|
dbclient "github.com/TicketsBot/GoPanel/database"
|
||||||
"github.com/TicketsBot/GoPanel/rpc"
|
"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()
|
var validate = validator.New()
|
||||||
|
|
||||||
func (p *panelBody) doValidations(ctx *gin.Context, guildId uint64) bool {
|
func (p *panelBody) doValidations(ctx *gin.Context, guildId uint64) bool {
|
||||||
err := validate.Struct(p)
|
if err := validate.Struct(p); err != nil {
|
||||||
if err != nil {
|
|
||||||
validationErrors, ok := err.(validator.ValidationErrors)
|
validationErrors, ok := err.(validator.ValidationErrors)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.JSON(500, utils.ErrorStr("An error occurred while validating the panel"))
|
ctx.JSON(500, utils.ErrorStr("An error occurred while validating the panel"))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted := "Your input contained the following errors:"
|
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
|
||||||
for _, validationError := range validationErrors {
|
|
||||||
formatted += fmt.Sprintf("\n%s", validationError.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
formatted = strings.TrimSuffix(formatted, "\n")
|
|
||||||
ctx.JSON(400, utils.ErrorStr(formatted))
|
ctx.JSON(400, utils.ErrorStr(formatted))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
dbclient "github.com/TicketsBot/GoPanel/database"
|
dbclient "github.com/TicketsBot/GoPanel/database"
|
||||||
"github.com/TicketsBot/GoPanel/utils"
|
"github.com/TicketsBot/GoPanel/utils"
|
||||||
"github.com/TicketsBot/GoPanel/utils/types"
|
"github.com/TicketsBot/GoPanel/utils/types"
|
||||||
@ -58,12 +57,7 @@ func CreateTag(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
formatted := "Your input contained the following errors:"
|
formatted := "Your input contained the following errors:\n" + utils.FormatValidationErrors(validationErrors)
|
||||||
for _, validationError := range validationErrors {
|
|
||||||
formatted += fmt.Sprintf("\n%s", validationError.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
formatted = strings.TrimSuffix(formatted, "\n")
|
|
||||||
ctx.JSON(400, utils.ErrorStr(formatted))
|
ctx.JSON(400, utils.ErrorStr(formatted))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
39
utils/validationutils.go
Normal file
39
utils/validationutils.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user