Skip to content

Commit

Permalink
feat: informative error message (#859)
Browse files Browse the repository at this point in the history
* err

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* wip

Signed-off-by: VoVAllen <allenzhou@tensorchord.ai>

* remove

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* add details for buildkitd error

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix lint

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* move to pkg/util

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

* fix

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>

Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
Signed-off-by: VoVAllen <allenzhou@tensorchord.ai>
  • Loading branch information
VoVAllen committed Sep 19, 2022
1 parent a3fd464 commit eecc7cf
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 56 deletions.
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)
} 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)
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 @@ -21,6 +21,7 @@ import (
"go.starlark.net/starlarkstruct"

"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/util/starlarkutil"
)

var (
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 := starlarkutil.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 @@ -21,6 +21,7 @@ import (
"go.starlark.net/starlarkstruct"

"github.com/tensorchord/envd/pkg/lang/ir"
"github.com/tensorchord/envd/pkg/util/starlarkutil"
)

var (
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 := starlarkutil.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 := starlarkutil.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 := starlarkutil.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 := starlarkutil.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 := starlarkutil.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 := starlarkutil.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 := starlarkutil.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 := starlarkutil.ToStringSlice(channel)
if err != nil {
return nil, err
}

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

Expand Down
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 @@ -23,6 +23,7 @@ import (

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

var (
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 := starlarkutil.ToStringSlice(commands)
if err != nil {
return nil, err
}

logger.Debugf("rule `%s` is invoked, commands=%v", ruleRun, goCommands)
Expand Down
38 changes: 38 additions & 0 deletions pkg/util/starlarkutil/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 starlarkutil

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
}

0 comments on commit eecc7cf

Please sign in to comment.