Skip to content

Commit

Permalink
[ws-daemon] Improve error when content init fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Weichel committed Feb 2, 2021
1 parent 53c5662 commit fd46a83
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
8 changes: 4 additions & 4 deletions components/content-service/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ func (s *Status) ToAPI() *csapi.GitStatus {
}
}

// ErrGitOpFailed is returned by GitWithOutput if the operation fails
// GitOpFailedError is returned by GitWithOutput if the operation fails
// e.g. returns with a non-zero exit code.
type ErrGitOpFailed struct {
type GitOpFailedError struct {
Subcommand string
Args []string
ExecErr error
Output string
}

func (e ErrGitOpFailed) Error() string {
func (e GitOpFailedError) Error() string {
return fmt.Sprintf("git %s %s failed (%v): %v", e.Subcommand, strings.Join(e.Args, " "), e.ExecErr, e.Output)
}

Expand Down Expand Up @@ -186,7 +186,7 @@ func (c *Client) GitWithOutput(ctx context.Context, subcommand string, args ...s

res, err := cmd.CombinedOutput()
if err != nil {
return nil, ErrGitOpFailed{
return nil, GitOpFailedError{
Args: args,
ExecErr: err,
Output: string(res),
Expand Down
2 changes: 1 addition & 1 deletion components/content-service/pkg/initializer/prebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (p *PrebuildInitializer) Run(ctx context.Context, mappings []archive.IDMapp
if git.IsWorkingCopy(p.Git.Location) {
out, err := p.Git.GitWithOutput(ctx, "stash", "push", "-u")
if err != nil {
var giterr git.ErrGitOpFailed
var giterr git.GitOpFailedError
if errors.As(err, &giterr) && strings.Contains(giterr.Output, "You do not have the initial commit yet") {
// git stash push returns a non-zero exit code if the repository does not have a single commit.
// In this case that's not an error though, hence we don't want to fail here.
Expand Down
10 changes: 10 additions & 0 deletions components/ws-daemon/pkg/content/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"

"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/tracing"
csapi "github.com/gitpod-io/gitpod/content-service/api"
"github.com/gitpod-io/gitpod/content-service/pkg/archive"
wsinit "github.com/gitpod-io/gitpod/content-service/pkg/initializer"
"github.com/gitpod-io/gitpod/content-service/pkg/storage"
"github.com/sirupsen/logrus"

"github.com/golang/protobuf/proto"
"github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -233,6 +235,13 @@ func RunInitializer(ctx context.Context, destination string, initializer *csapi.
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0. If it's 42, it was deliberate.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 42 {
return fmt.Errorf("content initializer failed")
}
}

return err
}

Expand All @@ -251,6 +260,7 @@ func RunInitializerChild() (err error) {
if err != nil {
return err
}
log.Log = logrus.WithFields(initmsg.OWI)

defer func() {
if err != nil {
Expand Down

0 comments on commit fd46a83

Please sign in to comment.