Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 85a3c8a

Browse files
committedDec 10, 2019
feat: categorize more flux errors with codes
This covers all of the packages that are in active use within the flux repository. It doesn't cover all of the packages, but enough that we should have good enough coverage for most code paths that get hit.
1 parent 4823b58 commit 85a3c8a

22 files changed

+150
-139
lines changed
 

‎compile.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package flux
22

33
import (
44
"context"
5-
stderrors "errors"
65
"fmt"
76
"path"
87
"regexp"
@@ -256,10 +255,10 @@ func Prelude() values.Scope {
256255
// RegisterPackage adds a builtin package
257256
func RegisterPackage(pkg *ast.Package) {
258257
if finalized {
259-
panic(stderrors.New("already finalized, cannot register builtin package"))
258+
panic(errors.New(codes.Internal, "already finalized, cannot register builtin package"))
260259
}
261260
if _, ok := builtinPackages[pkg.Path]; ok {
262-
panic(fmt.Errorf("duplicate builtin package %q", pkg.Path))
261+
panic(errors.Newf(codes.Internal, "duplicate builtin package %q", pkg.Path))
263262
}
264263
builtinPackages[pkg.Path] = pkg
265264
_, ok := stdlib.pkgs[pkg.Path]
@@ -282,7 +281,7 @@ func ReplacePackageValue(pkgpath, name string, value values.Value) {
282281

283282
func registerPackageValue(pkgpath, name string, value values.Value, replace bool) {
284283
if finalized {
285-
panic(stderrors.New("already finalized, cannot register builtin package value"))
284+
panic(errors.Newf(codes.Internal, "already finalized, cannot register builtin package value"))
286285
}
287286
packg, ok := stdlib.pkgs[pkgpath]
288287
if !ok {
@@ -292,9 +291,9 @@ func registerPackageValue(pkgpath, name string, value values.Value, replace bool
292291
stdlib.pkgs[pkgpath] = packg
293292
}
294293
if _, ok := packg.Get(name); ok && !replace {
295-
panic(fmt.Errorf("duplicate builtin package value %q %q", pkgpath, name))
294+
panic(errors.Newf(codes.Internal, "duplicate builtin package value %q %q", pkgpath, name))
296295
} else if !ok && replace {
297-
panic(fmt.Errorf("missing builtin package value %q %q", pkgpath, name))
296+
panic(errors.Newf(codes.Internal, "missing builtin package value %q %q", pkgpath, name))
298297
}
299298
packg.Set(name, value)
300299
}
@@ -318,7 +317,7 @@ func FunctionValueWithSideEffect(name string, c CreateOperationSpec, sig semanti
318317
func functionValue(name string, c CreateOperationSpec, sig semantic.FunctionPolySignature, sideEffects bool) values.Value {
319318
if c == nil {
320319
c = func(args Arguments, a *Administration) (OperationSpec, error) {
321-
return nil, fmt.Errorf("function %q is not implemented", name)
320+
return nil, errors.Newf(codes.Unimplemented, "function %q is not implemented", name)
322321
}
323322
}
324323
return &function{
@@ -409,7 +408,7 @@ func validatePackageBuiltins(pkg *interpreter.Package, astPkg *ast.Package) erro
409408
}
410409
})
411410
if len(missing) > 0 || len(extra) > 0 {
412-
return fmt.Errorf("missing builtin values %v, extra builtin values %v", missing, extra)
411+
return errors.Newf(codes.Internal, "missing builtin values %v, extra builtin values %v", missing, extra)
413412
}
414413
return nil
415414
}
@@ -621,7 +620,7 @@ func (a *Administration) AddParentFromArgs(args Arguments) error {
621620
}
622621
p, ok := parent.(*TableObject)
623622
if !ok {
624-
return fmt.Errorf("argument is not a table object: got %T", parent)
623+
return errors.Newf(codes.Invalid, "argument is not a table object: got %T", parent)
625624
}
626625
a.AddParent(p)
627626
return nil
@@ -753,7 +752,7 @@ func (a Arguments) GetRequiredTime(name string) (Time, error) {
753752
return Time{}, err
754753
}
755754
if !ok {
756-
return Time{}, fmt.Errorf("missing required keyword argument %q", name)
755+
return Time{}, errors.Newf(codes.Invalid, "missing required keyword argument %q", name)
757756
}
758757
return qt, nil
759758
}
@@ -772,7 +771,7 @@ func (a Arguments) GetRequiredDuration(name string) (Duration, error) {
772771
return ConvertDuration(0), err
773772
}
774773
if !ok {
775-
return ConvertDuration(0), fmt.Errorf("missing required keyword argument %q", name)
774+
return ConvertDuration(0), errors.Newf(codes.Invalid, "missing required keyword argument %q", name)
776775
}
777776
return d, nil
778777
}
@@ -793,7 +792,7 @@ func ToQueryTime(value values.Value) (Time, error) {
793792
Absolute: time.Unix(value.Int(), 0),
794793
}, nil
795794
default:
796-
return Time{}, fmt.Errorf("value is not a time, got %v", value.Type())
795+
return Time{}, errors.Newf(codes.Invalid, "value is not a time, got %v", value.Type())
797796
}
798797
}
799798

@@ -854,7 +853,7 @@ func insertPkg(pkg *ast.Package, pkgs map[string]*ast.Package, order []*ast.Pack
854853
for _, path := range imports {
855854
dep, ok := pkgs[path]
856855
if !ok {
857-
return nil, fmt.Errorf("unknown builtin package %q", path)
856+
return nil, errors.Newf(codes.Invalid, "unknown builtin package %q", path)
858857
}
859858
order, err = insertPkg(dep, pkgs, order)
860859
if err != nil {

‎compiler.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package flux
22

33
import (
44
"context"
5-
"fmt"
65

6+
"github.com/influxdata/flux/codes"
7+
"github.com/influxdata/flux/internal/errors"
78
"github.com/influxdata/flux/memory"
89
)
910

@@ -21,7 +22,7 @@ type CompilerMappings map[CompilerType]CreateCompiler
2122

2223
func (m CompilerMappings) Add(t CompilerType, c CreateCompiler) error {
2324
if _, ok := m[t]; ok {
24-
return fmt.Errorf("duplicate compiler mapping for %q", t)
25+
return errors.Newf(codes.Internal, "duplicate compiler mapping for %q", t)
2526
}
2627
m[t] = c
2728
return nil

‎compiler/compiler.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package compiler
22

33
import (
4-
stderrors "errors"
5-
"fmt"
6-
74
"github.com/influxdata/flux"
85
"github.com/influxdata/flux/codes"
96
"github.com/influxdata/flux/internal/errors"
@@ -16,7 +13,7 @@ func Compile(scope Scope, f *semantic.FunctionExpression, in semantic.Type) (Fun
1613
scope = NewScope()
1714
}
1815
if in.Nature() != semantic.Object {
19-
return nil, fmt.Errorf("function input must be an object @ %v", f.Location())
16+
return nil, errors.Newf(codes.Invalid, "function input must be an object @ %v", f.Location())
2017
}
2118
extern := values.BuildExternAssignments(f, scope)
2219

@@ -86,7 +83,7 @@ func compile(n semantic.Node, typeSol semantic.TypeSolution, scope Scope, funcEx
8683
body: body,
8784
}, nil
8885
case *semantic.ExpressionStatement:
89-
return nil, stderrors.New("statement does nothing, side effects are not supported by the compiler")
86+
return nil, errors.New(codes.Internal, "statement does nothing, side effects are not supported by the compiler")
9087
case *semantic.ReturnStatement:
9188
node, err := compile(n.Argument, typeSol, scope, funcExprs)
9289
if err != nil {
@@ -133,7 +130,7 @@ func compile(n semantic.Node, typeSol semantic.TypeSolution, scope Scope, funcEx
133130
}
134131
with, ok := node.(*identifierEvaluator)
135132
if !ok {
136-
return nil, stderrors.New("unknown identifier in with expression")
133+
return nil, errors.New(codes.Internal, "unknown identifier in with expression")
137134
}
138135
obj.with = with
139136

@@ -396,7 +393,7 @@ func compile(n semantic.Node, typeSol semantic.TypeSolution, scope Scope, funcEx
396393
body: body,
397394
}, nil
398395
default:
399-
return nil, fmt.Errorf("unknown semantic node of type %T", n)
396+
return nil, errors.Newf(codes.Internal, "unknown semantic node of type %T", n)
400397
}
401398
}
402399

@@ -435,13 +432,13 @@ type funcErr struct {
435432
Err error
436433
}
437434

438-
// Utility function for compiling an `fn` parameter for rename or drop/keep. In addition
435+
// CompileFnParam is a utility function for compiling an `fn` parameter for rename or drop/keep. In addition
439436
// to the function expression, it takes two types to verify the result against:
440437
// a single argument type, and a single return type.
441438
func CompileFnParam(fn *semantic.FunctionExpression, scope Scope, paramType, returnType semantic.Type) (Func, string, error) {
442439
compileCache := NewCompilationCache(fn, scope)
443440
if fn.Block.Parameters != nil && len(fn.Block.Parameters.List) != 1 {
444-
return nil, "", stderrors.New("function should only have a single parameter")
441+
return nil, "", errors.New(codes.Invalid, "function should only have a single parameter")
445442
}
446443
paramName := fn.Block.Parameters.List[0].Key.Name
447444

@@ -453,7 +450,7 @@ func CompileFnParam(fn *semantic.FunctionExpression, scope Scope, paramType, ret
453450
}
454451

455452
if compiled.Type() != returnType {
456-
return nil, "", fmt.Errorf("provided function does not evaluate to type %s", returnType.Nature())
453+
return nil, "", errors.Newf(codes.Invalid, "provided function does not evaluate to type %s", returnType.Nature())
457454
}
458455

459456
return compiled, paramName, nil

‎compiler/runtime.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package compiler
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"log"
87
"regexp"
98
"strings"
109

1110
"github.com/influxdata/flux/ast"
11+
"github.com/influxdata/flux/codes"
12+
"github.com/influxdata/flux/internal/errors"
1213
"github.com/influxdata/flux/semantic"
1314
"github.com/influxdata/flux/values"
1415
)
@@ -34,11 +35,11 @@ func (c compiledFn) validate(input values.Object) error {
3435
sig := c.fnType.FunctionSignature()
3536
properties := input.Type().Properties()
3637
if len(properties) != len(sig.Parameters) {
37-
return errors.New("mismatched parameters and properties")
38+
return errors.New(codes.Invalid, "mismatched parameters and properties")
3839
}
3940
for k, v := range sig.Parameters {
4041
if !values.AssignableTo(properties[k], v) {
41-
return fmt.Errorf("parameter %q has the wrong type, expected %v got %v", k, v, properties[k])
42+
return errors.Newf(codes.Invalid, "parameter %q has the wrong type, expected %v got %v", k, v, properties[k])
4243
}
4344
}
4445
return nil
@@ -281,7 +282,7 @@ func (e *logicalEvaluator) Eval(ctx context.Context, scope Scope) (values.Value,
281282
return values.NewBool(true), nil
282283
}
283284
default:
284-
panic(fmt.Errorf("unknown logical operator %v", e.operator))
285+
panic(errors.Newf(codes.Internal, "unknown logical operator %v", e.operator))
285286
}
286287

287288
r, err := e.right.Eval(ctx, scope)
@@ -370,7 +371,7 @@ func (e *unaryEvaluator) Eval(ctx context.Context, scope Scope) (values.Value, e
370371
case ast.SubtractionOperator, ast.NotOperator:
371372
// Fallthrough to below.
372373
default:
373-
return nil, fmt.Errorf("unknown unary operator: %s", e.op)
374+
return nil, errors.Newf(codes.Internal, "unknown unary operator: %s", e.op)
374375
}
375376

376377
// The subtraction operator falls through to here.

‎dialect.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package flux
22

3-
import "fmt"
3+
import (
4+
"github.com/influxdata/flux/codes"
5+
"github.com/influxdata/flux/internal/errors"
6+
)
47

58
// Dialect describes how to encode results.
69
type Dialect interface {
@@ -18,7 +21,7 @@ type DialectMappings map[DialectType]CreateDialect
1821

1922
func (m DialectMappings) Add(t DialectType, c CreateDialect) error {
2023
if _, ok := m[t]; ok {
21-
return fmt.Errorf("duplicate dialect mapping for %q", t)
24+
return errors.Newf(codes.Internal, "duplicate dialect mapping for %q", t)
2225
}
2326
m[t] = c
2427
return nil

‎operation.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (o *Operation) UnmarshalJSON(data []byte) error {
3939
func unmarshalOpSpec(k OperationKind, data []byte) (OperationSpec, error) {
4040
createOpSpec, ok := kindToOp[k]
4141
if !ok {
42-
return nil, fmt.Errorf("unknown operation spec kind %v", k)
42+
return nil, errors.Newf(codes.Invalid, "unknown operation spec kind %v", k)
4343
}
4444
spec := createOpSpec()
4545

@@ -86,7 +86,7 @@ var kindToOp = make(map[OperationKind]NewOperationSpec)
8686
// TODO:(nathanielc) make this part of RegisterMethod/RegisterFunction
8787
func RegisterOpSpec(k OperationKind, c NewOperationSpec) {
8888
if kindToOp[k] != nil {
89-
panic(fmt.Errorf("duplicate registration for operation kind %v", k))
89+
panic(errors.Newf(codes.Internal, "duplicate registration for operation kind %v", k))
9090
}
9191
kindToOp[k] = c
9292
}

‎semantic/analyze.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package semantic
22

33
import (
4-
"fmt"
5-
64
"github.com/influxdata/flux/ast"
75
"github.com/influxdata/flux/codes"
86
"github.com/influxdata/flux/internal/errors"
@@ -382,7 +380,7 @@ func analyzeFunctionExpression(arrow *ast.FunctionExpression) (*FunctionExpressi
382380
for i, p := range arrow.Params {
383381
ident, ok := p.Key.(*ast.Identifier)
384382
if !ok {
385-
return nil, fmt.Errorf("function params must be identifiers")
383+
return nil, errors.New(codes.Invalid, "function params must be identifiers")
386384
}
387385
key, err := analyzeIdentifier(ident)
388386
if err != nil {
@@ -456,11 +454,11 @@ func analyzeCallExpression(call *ast.CallExpression) (*CallExpression, error) {
456454
}
457455
var args *ObjectExpression
458456
if l := len(call.Arguments); l > 1 {
459-
return nil, fmt.Errorf("arguments are not a single object expression %v", args)
457+
return nil, errors.Newf(codes.Internal, "arguments are not a single object expression %v", args)
460458
} else if l == 1 {
461459
obj, ok := call.Arguments[0].(*ast.ObjectExpression)
462460
if !ok {
463-
return nil, fmt.Errorf("arguments not an object expression")
461+
return nil, errors.New(codes.Internal, "arguments not an object expression")
464462
}
465463
var err error
466464
args, err = analyzeObjectExpression(obj)

‎semantic/check.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package semantic
22

3-
import "fmt"
3+
import (
4+
"github.com/influxdata/flux/codes"
5+
"github.com/influxdata/flux/internal/errors"
6+
)
47

58
func runChecks(n Node, vars, opts map[string]bool) error {
69
// Check for options declared below package block.
@@ -43,7 +46,7 @@ func optionStatements(n Node) ([]*OptionStatement, error) {
4346
if err != nil {
4447
return nil, err
4548
}
46-
return nil, fmt.Errorf("option %q declared below package block at %v", name, errStmt.Location())
49+
return nil, errors.Newf(codes.Invalid, "option %q declared below package block at %v", name, errStmt.Location())
4750
}
4851
return stmts, nil
4952
}
@@ -56,11 +59,11 @@ func optionName(opt *OptionStatement) (string, error) {
5659
obj := n.Member.Object
5760
id, ok := obj.(*IdentifierExpression)
5861
if !ok {
59-
return "", fmt.Errorf("unsupported option qualifier %T", obj)
62+
return "", errors.Newf(codes.Invalid, "unsupported option qualifier %T", obj)
6063
}
6164
return id.Name + "." + n.Member.Property, nil
6265
default:
63-
return "", fmt.Errorf("unsupported assignment %T", n)
66+
return "", errors.Newf(codes.Internal, "unsupported assignment %T", n)
6467
}
6568
}
6669

@@ -98,10 +101,10 @@ func optionReAssignments(stmts []*OptionStatement, vars, options map[string]bool
98101
return err
99102
}
100103
if options[name] {
101-
return fmt.Errorf("option %q redeclared at %v", name, stmt.Location())
104+
return errors.Newf(codes.Invalid, "option %q redeclared at %v", name, stmt.Location())
102105
}
103106
if vars[name] {
104-
return fmt.Errorf("cannot declare option %q at %v; variable with same name already declared", name, stmt.Location())
107+
return errors.Newf(codes.Invalid, "cannot declare option %q at %v; variable with same name already declared", name, stmt.Location())
105108
}
106109
options[name] = true
107110
}
@@ -125,11 +128,11 @@ func varReAssignments(n Node, vars, opts map[string]bool) error {
125128
Walk(NewScopedVisitor(visitor), n)
126129
if varDec != nil {
127130
name := varDec.Identifier.Name
128-
return fmt.Errorf("var %q redeclared at %v", name, varDec.Location())
131+
return errors.Newf(codes.Invalid, "var %q redeclared at %v", name, varDec.Location())
129132
}
130133
if optDec != nil {
131134
name := optDec.Identifier.Name
132-
return fmt.Errorf("cannot declare variable %q at %v; option with same name already declared", name, optDec.Location())
135+
return errors.Newf(codes.Invalid, "cannot declare variable %q at %v; option with same name already declared", name, optDec.Location())
133136
}
134137
return nil
135138
}
@@ -193,7 +196,7 @@ func optionDependencies(stmts []*OptionStatement, options map[string]bool) error
193196
name := n.Identifier.Name
194197
Walk(NewScopedVisitor(visitor), n.Init)
195198
if dep != nil {
196-
return fmt.Errorf("option dependency: option %q depends on option %q defined in the same package at %v", name, dep.Name, dep.Location())
199+
return errors.Newf(codes.Invalid, "option dependency: option %q depends on option %q defined in the same package at %v", name, dep.Name, dep.Location())
197200
}
198201
}
199202
return nil

0 commit comments

Comments
 (0)
Please sign in to comment.