summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/validationfile/blocks/errors.go
blob: b30ef7abd1a5615b9aaaabb519516ee41adeef29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package blocks

import (
	"errors"
	"fmt"
	"regexp"
	"strconv"
	"strings"

	"github.com/authzed/spicedb/pkg/spiceerrors"
)

var (
	yamlLineRegex      = regexp.MustCompile(`line ([0-9]+): (.+)`)
	yamlUnmarshalRegex = regexp.MustCompile("cannot unmarshal !!str `([^`]+)...`")
)

func convertYamlError(err error) error {
	linePieces := yamlLineRegex.FindStringSubmatch(err.Error())
	if len(linePieces) == 3 {
		lineNumber, parseErr := strconv.ParseUint(linePieces[1], 10, 32)
		if parseErr != nil {
			lineNumber = 0
		}

		message := linePieces[2]
		source := ""
		unmarshalPieces := yamlUnmarshalRegex.FindStringSubmatch(message)
		if len(unmarshalPieces) == 2 {
			source = unmarshalPieces[1]
			if strings.Contains(source, " ") {
				source, _, _ = strings.Cut(source, " ")
			}

			message = fmt.Sprintf("unexpected value `%s`", source)
		}

		return spiceerrors.NewWithSourceError(
			errors.New(message),
			source,
			lineNumber,
			0,
		)
	}

	return err
}