aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlparser/validator/error.go
blob: f354dee594e7a7bb1e2da8fe422292286ea9209c (plain) (blame)
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
48
49
50
51
52
53
54
55
package validator

import (
	"fmt"

	"github.com/vektah/gqlparser/ast"
	"github.com/vektah/gqlparser/gqlerror"
)

type ErrorOption func(err *gqlerror.Error)

func Message(msg string, args ...interface{}) ErrorOption {
	return func(err *gqlerror.Error) {
		err.Message += fmt.Sprintf(msg, args...)
	}
}

func At(position *ast.Position) ErrorOption {
	return func(err *gqlerror.Error) {
		if position == nil {
			return
		}
		err.Locations = append(err.Locations, gqlerror.Location{
			Line:   position.Line,
			Column: position.Column,
		})
		if position.Src.Name != "" {
			err.SetFile(position.Src.Name)
		}
	}
}

func SuggestListQuoted(prefix string, typed string, suggestions []string) ErrorOption {
	suggested := SuggestionList(typed, suggestions)
	return func(err *gqlerror.Error) {
		if len(suggested) > 0 {
			err.Message += " " + prefix + " " + QuotedOrList(suggested...) + "?"
		}
	}
}

func SuggestListUnquoted(prefix string, typed string, suggestions []string) ErrorOption {
	suggested := SuggestionList(typed, suggestions)
	return func(err *gqlerror.Error) {
		if len(suggested) > 0 {
			err.Message += " " + prefix + " " + OrList(suggested...) + "?"
		}
	}
}

func Suggestf(suggestion string, args ...interface{}) ErrorOption {
	return func(err *gqlerror.Error) {
		err.Message += " Did you mean " + fmt.Sprintf(suggestion, args...) + "?"
	}
}