1package helpers
2
3import (
4 "math/rand"
5
6 "github.com/labstack/echo/v4"
7)
8
9// This will confirm to the regex in the application if 5 chars are used for each side of the -
10// /^[A-Z2-7]{5}-[A-Z2-7]{5}$/
11var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
12
13func InputError(e echo.Context, custom *string) error {
14 msg := "InvalidRequest"
15 if custom != nil {
16 msg = *custom
17 }
18 return genericError(e, 400, msg)
19}
20
21func ServerError(e echo.Context, suffix *string) error {
22 msg := "Internal server error"
23 if suffix != nil {
24 msg += ". " + *suffix
25 }
26 return genericError(e, 400, msg)
27}
28
29func genericError(e echo.Context, code int, msg string) error {
30 return e.JSON(code, map[string]string{
31 "error": msg,
32 })
33}
34
35func RandomVarchar(length int) string {
36 b := make([]rune, length)
37 for i := range b {
38 b[i] = letters[rand.Intn(len(letters))]
39 }
40 return string(b)
41}