cubefs

Форк
0
78 строк · 2.0 Кб
1
package gqlerrors
2

3
import (
4
	"fmt"
5
	"reflect"
6

7
	"github.com/graphql-go/graphql/language/ast"
8
	"github.com/graphql-go/graphql/language/location"
9
	"github.com/graphql-go/graphql/language/source"
10
)
11

12
type Error struct {
13
	Message       string
14
	Stack         string
15
	Nodes         []ast.Node
16
	Source        *source.Source
17
	Positions     []int
18
	Locations     []location.SourceLocation
19
	OriginalError error
20
	Path          []interface{}
21
}
22

23
// implements Golang's built-in `error` interface
24
func (g Error) Error() string {
25
	return fmt.Sprintf("%v", g.Message)
26
}
27

28
func NewError(message string, nodes []ast.Node, stack string, source *source.Source, positions []int, origError error) *Error {
29
	return newError(message, nodes, stack, source, positions, nil, origError)
30
}
31

32
func NewErrorWithPath(message string, nodes []ast.Node, stack string, source *source.Source, positions []int, path []interface{}, origError error) *Error {
33
	return newError(message, nodes, stack, source, positions, path, origError)
34
}
35

36
func newError(message string, nodes []ast.Node, stack string, source *source.Source, positions []int, path []interface{}, origError error) *Error {
37
	if stack == "" && message != "" {
38
		stack = message
39
	}
40
	if source == nil {
41
		for _, node := range nodes {
42
			// get source from first node
43
			if node == nil || reflect.ValueOf(node).IsNil() {
44
				continue
45
			}
46
			if node.GetLoc() != nil {
47
				source = node.GetLoc().Source
48
			}
49
			break
50
		}
51
	}
52
	if len(positions) == 0 && len(nodes) > 0 {
53
		for _, node := range nodes {
54
			if node == nil || reflect.ValueOf(node).IsNil() {
55
				continue
56
			}
57
			if node.GetLoc() == nil {
58
				continue
59
			}
60
			positions = append(positions, node.GetLoc().Start)
61
		}
62
	}
63
	locations := []location.SourceLocation{}
64
	for _, pos := range positions {
65
		loc := location.GetLocation(source, pos)
66
		locations = append(locations, loc)
67
	}
68
	return &Error{
69
		Message:       message,
70
		Stack:         stack,
71
		Nodes:         nodes,
72
		Source:        source,
73
		Positions:     positions,
74
		Locations:     locations,
75
		OriginalError: origError,
76
		Path:          path,
77
	}
78
}
79

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.