1
1
package expr
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
5
+ "net/http"
6
6
"reflect"
7
7
"strconv"
8
8
"strings"
9
9
10
10
"github.com/grafana/metrictank/api/models"
11
+ "github.com/grafana/metrictank/errors"
11
12
"github.com/grafana/metrictank/util"
12
13
log "github.com/sirupsen/logrus"
13
14
)
14
15
15
16
var (
16
- ErrMissingArg = errors .New ("argument missing" )
17
- ErrTooManyArg = errors .New ("too many arguments" )
18
- ErrMissingTimeseries = errors .New ("missing time series argument" )
19
- ErrWildcardNotAllowed = errors .New ("found wildcard where series expected" )
20
- ErrMissingExpr = errors .New ("missing expression" )
21
- ErrMissingComma = errors .New ("missing comma" )
22
- ErrMissingQuote = errors .New ("missing quote" )
23
- ErrUnexpectedCharacter = errors .New ("unexpected character" )
24
- ErrIllegalCharacter = errors .New ("illegal character for function name" )
17
+ ErrMissingArg = errors .NewBadRequest ("argument missing" )
18
+ ErrTooManyArg = errors .NewBadRequest ("too many arguments" )
19
+ ErrMissingTimeseries = errors .NewBadRequest ("missing time series argument" )
20
+ ErrWildcardNotAllowed = errors .NewBadRequest ("found wildcard where series expected" )
21
+ ErrMissingExpr = errors .NewBadRequest ("missing expression" )
22
+ ErrMissingComma = errors .NewBadRequest ("missing comma" )
23
+ ErrMissingQuote = errors .NewBadRequest ("missing quote" )
24
+ ErrUnexpectedCharacter = errors .NewBadRequest ("unexpected character" )
25
+ ErrIllegalCharacter = errors .NewBadRequest ("illegal character for function name" )
25
26
)
26
27
27
28
type ErrBadArgument struct {
@@ -33,6 +34,10 @@ func (e ErrBadArgument) Error() string {
33
34
return fmt .Sprintf ("argument bad type. expected %s - got %s" , e .exp , e .got )
34
35
}
35
36
37
+ func (e ErrBadArgument ) Code () int {
38
+ return http .StatusBadRequest
39
+ }
40
+
36
41
type ErrBadArgumentStr struct {
37
42
exp string
38
43
got string
@@ -42,12 +47,20 @@ func (e ErrBadArgumentStr) Error() string {
42
47
return fmt .Sprintf ("argument bad type. expected %s - got %s" , e .exp , e .got )
43
48
}
44
49
50
+ func (e ErrBadArgumentStr ) Code () int {
51
+ return http .StatusBadRequest
52
+ }
53
+
45
54
type ErrUnknownFunction string
46
55
47
56
func (e ErrUnknownFunction ) Error () string {
48
57
return fmt .Sprintf ("unknown function %q" , string (e ))
49
58
}
50
59
60
+ func (e ErrUnknownFunction ) Code () int {
61
+ return http .StatusBadRequest
62
+ }
63
+
51
64
type ErrUnknownKwarg struct {
52
65
key string
53
66
}
@@ -56,6 +69,10 @@ func (e ErrUnknownKwarg) Error() string {
56
69
return fmt .Sprintf ("unknown keyword argument %q" , e .key )
57
70
}
58
71
72
+ func (e ErrUnknownKwarg ) Code () int {
73
+ return http .StatusBadRequest
74
+ }
75
+
59
76
type ErrBadKwarg struct {
60
77
key string
61
78
exp Arg
@@ -66,6 +83,10 @@ func (e ErrBadKwarg) Error() string {
66
83
return fmt .Sprintf ("keyword argument %q bad type. expected %T - got %s" , e .key , e .exp , e .got )
67
84
}
68
85
86
+ func (e ErrBadKwarg ) Code () int {
87
+ return http .StatusBadRequest
88
+ }
89
+
69
90
type ErrKwargSpecifiedTwice struct {
70
91
key string
71
92
}
@@ -74,6 +95,10 @@ func (e ErrKwargSpecifiedTwice) Error() string {
74
95
return fmt .Sprintf ("keyword argument %q specified twice" , e .key )
75
96
}
76
97
98
+ func (e ErrKwargSpecifiedTwice ) Code () int {
99
+ return http .StatusBadRequest
100
+ }
101
+
77
102
type MetricRequest struct {
78
103
Metric string
79
104
From int32
@@ -90,7 +115,7 @@ func ParseMany(targets []string) ([]*expr, error) {
90
115
return nil , err
91
116
}
92
117
if leftover != "" {
93
- return nil , fmt . Errorf ("failed to parse %q fully. got leftover %q" , target , leftover )
118
+ return nil , errors . NewBadRequestf ("failed to parse %q fully. got leftover %q" , target , leftover )
94
119
}
95
120
out = append (out , e )
96
121
}
0 commit comments