error.go

 1package validator
 2
 3import (
 4	"fmt"
 5
 6	"github.com/vektah/gqlparser/ast"
 7	"github.com/vektah/gqlparser/gqlerror"
 8)
 9
10type ErrorOption func(err *gqlerror.Error)
11
12func Message(msg string, args ...interface{}) ErrorOption {
13	return func(err *gqlerror.Error) {
14		err.Message += fmt.Sprintf(msg, args...)
15	}
16}
17
18func At(position *ast.Position) ErrorOption {
19	return func(err *gqlerror.Error) {
20		if position == nil {
21			return
22		}
23		err.Locations = append(err.Locations, gqlerror.Location{
24			Line:   position.Line,
25			Column: position.Column,
26		})
27		if position.Src.Name != "" {
28			err.SetFile(position.Src.Name)
29		}
30	}
31}
32
33func SuggestListQuoted(prefix string, typed string, suggestions []string) ErrorOption {
34	suggested := SuggestionList(typed, suggestions)
35	return func(err *gqlerror.Error) {
36		if len(suggested) > 0 {
37			err.Message += " " + prefix + " " + QuotedOrList(suggested...) + "?"
38		}
39	}
40}
41
42func SuggestListUnquoted(prefix string, typed string, suggestions []string) ErrorOption {
43	suggested := SuggestionList(typed, suggestions)
44	return func(err *gqlerror.Error) {
45		if len(suggested) > 0 {
46			err.Message += " " + prefix + " " + OrList(suggested...) + "?"
47		}
48	}
49}
50
51func Suggestf(suggestion string, args ...interface{}) ErrorOption {
52	return func(err *gqlerror.Error) {
53		err.Message += " Did you mean " + fmt.Sprintf(suggestion, args...) + "?"
54	}
55}