/
errors.go
127 lines (106 loc) · 2.87 KB
/
errors.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package query
import (
"fmt"
"github.com/juju/errors"
)
// InvalidIdentifierError creates an invalid error.
type InvalidIdentifierError struct {
name string
err error
scope Scope
}
func (e *InvalidIdentifierError) Error() string {
if e.err == nil {
return ""
}
return e.err.Error()
}
// Name returns the name associated with the identifier error.
func (e *InvalidIdentifierError) Name() string {
return e.name
}
func (e *InvalidIdentifierError) Scope() Scope {
return e.scope
}
// ErrInvalidIdentifier defines a sentinel error for invalid identifiers.
func ErrInvalidIdentifier(name string, scope Scope) error {
return &InvalidIdentifierError{
name: name,
scope: scope,
}
}
// IsInvalidIdentifierErr returns if the error is an ErrInvalidIdentifier error
func IsInvalidIdentifierErr(err error) bool {
err = errors.Cause(err)
_, ok := err.(*InvalidIdentifierError)
return ok
}
// RuntimeError creates an invalid error.
type RuntimeError struct {
err error
}
func (e *RuntimeError) Error() string {
return e.err.Error()
}
// RuntimeErrorf defines a sentinel error for runtime errors.
func RuntimeErrorf(msg string, args ...interface{}) error {
return &RuntimeError{
err: errors.Errorf(msg, args...),
}
}
// IsRuntimeError returns if the error is an ErrInvalidIndex error
func IsRuntimeError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*RuntimeError)
return ok
}
// RuntimeSyntaxError creates an invalid error.
type RuntimeSyntaxError struct {
err error
Name string
Options []string
}
func (e *RuntimeSyntaxError) Error() string {
return e.err.Error()
}
// ErrRuntimeSyntax defines a sentinel error for runtime syntax error.
func ErrRuntimeSyntax(msg, name string, options []string) error {
return &RuntimeSyntaxError{
err: errors.Errorf(msg),
Name: name,
Options: options,
}
}
// IsRuntimeSyntaxError returns if the error is an ErrInvalidIndex error
func IsRuntimeSyntaxError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*RuntimeSyntaxError)
return ok
}
// SyntaxError creates an invalid error.
type SyntaxError struct {
Pos Position
TokenType TokenType
Expectations []TokenType
}
func (e *SyntaxError) Error() string {
if len(e.Expectations) == 0 {
return fmt.Sprintf("Syntax Error: %v invalid character '%s' found", e.Pos, e.TokenType)
}
return fmt.Sprintf("Syntax Error: %v expected token to be %s, got %s instead", e.Pos, e.Expectations[0], e.TokenType)
}
func ErrSyntaxError(pos Position, tokenType TokenType, expectations ...TokenType) error {
return &SyntaxError{
Pos: pos,
TokenType: tokenType,
Expectations: expectations,
}
}
// IsSyntaxError returns if the error is an ErrSyntaxError error
func IsSyntaxError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*SyntaxError)
return ok
}