Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: informative error message #859

Merged
merged 12 commits into from
Sep 19, 2022
9 changes: 6 additions & 3 deletions cmd/envd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cockroachdb/errors"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/syntax"

Expand All @@ -44,18 +45,20 @@ func handleErr(err error) {
}

if viper.GetBool(flag.FlagDebug) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
fmt.Fprintf(os.Stderr, "error: %+v\n", err)
}

rootCause := errors.Cause(err)
var evalErr *starlark.EvalError
var syntaxErr *syntax.Error
var resolveErr resolve.ErrorList
if ok := errors.As(err, &evalErr); ok {
fmt.Fprintln(os.Stderr, evalErr.Backtrace())
} else if ok := errors.As(err, &syntaxErr); ok {
fmt.Fprintln(os.Stderr, syntaxErr)
} else if ok := errors.As(err, &resolveErr); ok {
fmt.Fprintf(os.Stderr, "%+v\n", resolveErr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use %w to format errors

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

} else {
fmt.Fprintf(os.Stderr, "error: %v\n", rootCause)
fmt.Fprintf(os.Stderr, "error: %v\n", errors.Cause(err))
}
os.Exit(1)
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package builder

import (
"context"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -72,6 +73,15 @@ type Options struct {
UseHTTPProxy bool
}

type BuildkitdErr struct {
err error
}

func (e *BuildkitdErr) Error() string {
return e.err.Error()
}
func (e *BuildkitdErr) Format(s fmt.State, verb rune) { errors.FormatError(e, s, verb) }

type generalBuilder struct {
Options
manifestCodeHash string
Expand Down Expand Up @@ -280,7 +290,8 @@ func (b generalBuilder) build(ctx context.Context, pw progresswriter.Writer) err
}
_, err := b.Client.Build(ctx, solveOpt, "envd", b.BuildFunc(), pw.Status())
if err != nil {
err = errors.Wrap(err, "failed to solve LLB")
err = errors.Wrap(&BuildkitdErr{err: err}, "Buildkit error")
logrus.Errorf("%+v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use %w to format errors

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional. %+v is a feature of cockroachdb/errors to print the full error messages including stacktrace.

return err
}
b.logger.Debug("llb def is solved successfully")
Expand Down
9 changes: 4 additions & 5 deletions pkg/lang/frontend/starlark/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"

"github.com/tensorchord/envd/pkg/lang/frontend/starlark/starlarkutils"
"github.com/tensorchord/envd/pkg/lang/ir"
)

Expand Down Expand Up @@ -202,11 +203,9 @@ func ruleFuncEntrypoint(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

argList := []string{}
if argv != nil {
for i := 0; i < argv.Len(); i++ {
argList = append(argList, argv.Index(i).(starlark.String).GoString())
}
argList, err := starlarkutils.ToStringSlice(argv)
if err != nil {
return nil, err
}

logger.Debugf("user defined entrypoints: {%s}\n", argList)
Expand Down
69 changes: 27 additions & 42 deletions pkg/lang/frontend/starlark/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"

"github.com/tensorchord/envd/pkg/lang/frontend/starlark/starlarkutils"
"github.com/tensorchord/envd/pkg/lang/ir"
)

Expand Down Expand Up @@ -51,26 +52,22 @@ func ruleFuncPyPIPackage(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

nameList := []string{}
if name != nil {
for i := 0; i < name.Len(); i++ {
nameList = append(nameList, name.Index(i).(starlark.String).GoString())
}
nameList, err := starlarkutils.ToStringSlice(name)
if err != nil {
return nil, err
}

requirementsFileStr := requirementsFile.GoString()

localWheels := []string{}
if wheels != nil {
for i := 0; i < wheels.Len(); i++ {
localWheels = append(localWheels, wheels.Index(i).(starlark.String).GoString())
}
localWheels, err := starlarkutils.ToStringSlice(wheels)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, name=%v, requirements=%s, local_wheels=%s",
rulePyPIPackage, nameList, requirementsFileStr, localWheels)

err := ir.PyPIPackage(nameList, requirementsFileStr, localWheels)
err = ir.PyPIPackage(nameList, requirementsFileStr, localWheels)
return starlark.None, err
}

Expand All @@ -83,11 +80,9 @@ func ruleFuncRPackage(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

nameList := []string{}
if name != nil {
for i := 0; i < name.Len(); i++ {
nameList = append(nameList, name.Index(i).(starlark.String).GoString())
}
nameList, err := starlarkutils.ToStringSlice(name)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, name=%v", ruleRPackage, nameList)
Expand All @@ -105,13 +100,10 @@ func ruleFuncJulia(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

nameList := []string{}
if name != nil {
for i := 0; i < name.Len(); i++ {
nameList = append(nameList, name.Index(i).(starlark.String).GoString())
}
nameList, err := starlarkutils.ToStringSlice(name)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, name=%v", ruleJulia, nameList)
ir.JuliaPackage(nameList)

Expand All @@ -127,11 +119,9 @@ func ruleFuncSystemPackage(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

nameList := []string{}
if name != nil {
for i := 0; i < name.Len(); i++ {
nameList = append(nameList, name.Index(i).(starlark.String).GoString())
}
nameList, err := starlarkutils.ToStringSlice(name)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, name=%v", ruleSystemPackage, nameList)
Expand Down Expand Up @@ -168,11 +158,9 @@ func ruleFuncVSCode(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

pluginList := []string{}
if plugins != nil {
for i := 0; i < plugins.Len(); i++ {
pluginList = append(pluginList, plugins.Index(i).(starlark.String).GoString())
}
pluginList, err := starlarkutils.ToStringSlice(plugins)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, plugins=%v", ruleVSCode, pluginList)
Expand All @@ -193,19 +181,16 @@ func ruleFuncConda(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

nameList := []string{}
if name != nil {
for i := 0; i < name.Len(); i++ {
nameList = append(nameList, name.Index(i).(starlark.String).GoString())
}
nameList, err := starlarkutils.ToStringSlice(name)
if err != nil {
return nil, err
}

channelList := []string{}
if channel != nil {
for i := 0; i < channel.Len(); i++ {
channelList = append(channelList, channel.Index(i).(starlark.String).GoString())
}
channelList, err := starlarkutils.ToStringSlice(channel)
if err != nil {
return nil, err
}

envFileStr := envFile.GoString()
if envFileStr != "" {

Expand Down
38 changes: 38 additions & 0 deletions pkg/lang/frontend/starlark/starlarkutils/stringslice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package starlarkutils

import (
"github.com/cockroachdb/errors"

"go.starlark.net/starlark"
)

func ToStringSlice(v *starlark.List) ([]string, error) {
if v == nil {
return []string{}, nil
}

s := []string{}
for i := 0; i < v.Len(); i++ {
str, ok := starlark.AsString(v.Index(i))
if !ok {
return nil, errors.Newf("Conversion failed, expect string type, but got %s as %s", v.Index(i), v.Index(i).Type())
}
s = append(s, str)
}

return s, nil
}
9 changes: 4 additions & 5 deletions pkg/lang/frontend/starlark/universe/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.starlark.net/starlarkstruct"

"github.com/tensorchord/envd/pkg/lang/frontend/starlark/builtin"
"github.com/tensorchord/envd/pkg/lang/frontend/starlark/starlarkutils"
"github.com/tensorchord/envd/pkg/lang/ir"
)

Expand Down Expand Up @@ -68,11 +69,9 @@ func ruleFuncRun(thread *starlark.Thread, _ *starlark.Builtin,
return nil, err
}

goCommands := []string{}
if commands != nil {
for i := 0; i < commands.Len(); i++ {
goCommands = append(goCommands, commands.Index(i).(starlark.String).GoString())
}
goCommands, err := starlarkutils.ToStringSlice(commands)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, commands=%v", ruleRun, goCommands)
Expand Down