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: add error tracing #479

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/juju/errors"
"github.com/sirupsen/logrus"

"kraftkit.sh/log"
Expand All @@ -38,12 +38,12 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..
dst = filepath.ToSlash(dst)

if dst == "" {
return fmt.Errorf("cannot tar file with no specified destination")
return errors.New("cannot tar file with no specified destination")
} else if dst[0] == '/' {
dst = dst[1:]
}
if strings.HasSuffix(dst, "/") {
return fmt.Errorf("attempting to use TarFileWriter with directory")
return errors.New("attempting to use TarFileWriter with directory")
}

aopts := ArchiveOptions{}
Expand All @@ -55,7 +55,7 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..

fi, err := os.Stat(src)
if err != nil {
return fmt.Errorf("fail to stat %s: %v", src, err)
return errors.Annotatef(err, "fail to stat %s", src)
}

var link string
Expand All @@ -68,7 +68,7 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..

header, err := tar.FileInfoHeader(fi, link)
if err != nil {
return fmt.Errorf("%s: %w", src, err)
return errors.Annotatef(err, "%s", src)
}

header.Name = dst
Expand All @@ -85,7 +85,7 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..
}

if err := tw.WriteHeader(header); err != nil {
return fmt.Errorf("tar: %w", err)
return errors.Annotate(err, "tar")
}

if mode.IsRegular() {
Expand All @@ -96,14 +96,14 @@ func TarFileWriter(ctx context.Context, src, dst string, tw *tar.Writer, opts ..

fp, err := os.Open(src)
if err != nil {
return fmt.Errorf("fail to open file %s: %v", src, err)
return errors.Annotatef(err, "fail to open file %s", src)
}

buf := bufPool.Get().(*[]byte)
defer bufPool.Put(buf)

if _, err := io.CopyBuffer(tw, fp, *buf); err != nil {
return fmt.Errorf("failed to copy to %s: %w", src, err)
return errors.Annotatef(err, "failed to copy to %s", src)
}

if err := fp.Close(); err != nil {
Expand All @@ -126,7 +126,7 @@ func TarFileTo(ctx context.Context, src, dst, out string, opts ...ArchiveOption)

fp, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("could not create tarball file: %s: %v", out, err)
return errors.Annotatef(err, "could not create tarball file: %s", out)
}

var tw *tar.Writer
Expand Down Expand Up @@ -164,7 +164,7 @@ func TarFileTo(ctx context.Context, src, dst, out string, opts ...ArchiveOption)
func TarFile(ctx context.Context, src, prefix, out string, opts ...ArchiveOption) error {
fp, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("could not create tarball file: %s: %v", out, err)
return errors.Annotatef(err, "could not create tarball file: %s", out)
}

tw := tar.NewWriter(fp)
Expand All @@ -183,7 +183,7 @@ func TarFile(ctx context.Context, src, prefix, out string, opts ...ArchiveOption
func TarDir(ctx context.Context, root, prefix, out string, opts ...ArchiveOption) error {
fp, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("could not create tarball file: %s: %v", out, err)
return errors.Annotatef(err, "could not create tarball file: %s", out)
}

tw := tar.NewWriter(fp)
Expand Down
17 changes: 9 additions & 8 deletions archive/unarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package archive
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/juju/errors"
)

// Unarchive takes an input src file and determines (based on its extension)
Expand All @@ -21,21 +22,21 @@ func Unarchive(src, dst string, opts ...UnarchiveOption) error {
return UntarGz(src, dst, opts...)
}

return fmt.Errorf("unrecognized extension: %s", filepath.Base(src))
return errors.Errorf("unrecognized extension: %s", filepath.Base(src))
}

// UntarGz unarchives a tarball which has been gzip compressed
func UntarGz(src, dst string, opts ...UnarchiveOption) error {
f, err := os.Open(src)
if err != nil {
return fmt.Errorf("could not open file: %v", err)
return errors.Annotate(err, "could not open file")
}

defer f.Close()

gzipReader, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("could not open gzip reader: %v", err)
return errors.Annotate(err, "could not open gzip reader")
}

return Untar(gzipReader, dst, opts...)
Expand Down Expand Up @@ -78,23 +79,23 @@ func Untar(src io.Reader, dst string, opts ...UnarchiveOption) error {
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(path, info.Mode()); err != nil {
return fmt.Errorf("could not create directory: %v", err)
return errors.Annotate(err, "could not create directory")
}

case tar.TypeReg:
// Create parent path if it does not exist
if err := os.MkdirAll(filepath.Dir(path), info.Mode()); err != nil {
return fmt.Errorf("could not create directory: %v", err)
return errors.Annotate(err, "could not create directory")
}

newFile, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return fmt.Errorf("could not create file: %v", err)
return errors.Annotate(err, "could not create file")
}

if _, err := io.Copy(newFile, tr); err != nil {
newFile.Close()
return fmt.Errorf("could not copy file: %v", err)
return errors.Annotate(err, "could not copy file")
}

newFile.Close()
Expand Down
23 changes: 12 additions & 11 deletions cmd/kraft/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

"github.com/MakeNowJust/heredoc"
jujuerrors "github.com/juju/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -112,9 +113,9 @@ func (opts *Build) Pre(cmd *cobra.Command, args []string) error {
// Initialize at least the configuration options for a project
opts.project, err = app.NewProjectFromOptions(ctx, popts...)
if err != nil && errors.Is(err, app.ErrNoKraftfile) {
return fmt.Errorf("cannot build project directory without a Kraftfile")
return jujuerrors.New("cannot build project directory without a Kraftfile")
} else if err != nil {
return fmt.Errorf("could not initialize project directory: %w", err)
return jujuerrors.Annotate(err, "could not initialize project directory")
}

return nil
Expand Down Expand Up @@ -146,11 +147,11 @@ func (opts *Build) pull(ctx context.Context, project app.Application, workdir st
}

if len(packages) == 0 {
return fmt.Errorf("could not find: %s",
return jujuerrors.Errorf("could not find: %s",
unikraft.TypeNameVersion(opts.project.Template()),
)
} else if len(packages) > 1 {
return fmt.Errorf("too many options for %s",
return jujuerrors.Errorf("too many options for %s",
unikraft.TypeNameVersion(opts.project.Template()),
)
}
Expand All @@ -173,7 +174,7 @@ func (opts *Build) pull(ctx context.Context, project app.Application, workdir st
}

if err := treemodel.Start(); err != nil {
return fmt.Errorf("could not complete search: %v", err)
return jujuerrors.Annotate(err, "could not complete search")
}

proc := paraprogress.NewProcess(
Expand Down Expand Up @@ -206,7 +207,7 @@ func (opts *Build) pull(ctx context.Context, project app.Application, workdir st
}

if err := paramodel.Start(); err != nil {
return fmt.Errorf("could not pull all components: %v", err)
return jujuerrors.Annotate(err, "could not pull all components")
}
}

Expand Down Expand Up @@ -276,11 +277,11 @@ func (opts *Build) pull(ctx context.Context, project app.Application, workdir st
}

if len(p) == 0 {
return fmt.Errorf("could not find: %s",
return jujuerrors.Errorf("could not find: %s",
unikraft.TypeNameVersion(component),
)
} else if len(p) > 1 {
return fmt.Errorf("too many options for %s",
return jujuerrors.Errorf("too many options for %s",
unikraft.TypeNameVersion(component),
)
}
Expand All @@ -306,7 +307,7 @@ func (opts *Build) pull(ctx context.Context, project app.Application, workdir st
}

if err := treemodel.Start(); err != nil {
return fmt.Errorf("could not complete search: %v", err)
return jujuerrors.Annotate(err, "could not complete search")
}
}

Expand Down Expand Up @@ -344,7 +345,7 @@ func (opts *Build) pull(ctx context.Context, project app.Application, workdir st
}

if err := paramodel.Start(); err != nil {
return fmt.Errorf("could not pull all components: %v", err)
return jujuerrors.Annotate(err, "could not pull all components")
}
}

Expand Down Expand Up @@ -374,7 +375,7 @@ func (opts *Build) Run(cmd *cobra.Command, args []string) error {
}

if len(selected) == 0 {
return fmt.Errorf("no targets selected to build")
return jujuerrors.New("no targets selected to build")
}

norender := log.LoggerTypeFromString(config.G[config.KraftKit](ctx).Log.Type) != log.FANCY
Expand Down
16 changes: 8 additions & 8 deletions cmd/kraft/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package events

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
Expand All @@ -15,6 +14,7 @@ import (
"time"

"github.com/MakeNowJust/heredoc"
"github.com/juju/errors"
"github.com/spf13/cobra"

machineapi "kraftkit.sh/api/machine/v1alpha1"
Expand Down Expand Up @@ -78,7 +78,7 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {
platform, mode, err = mplatform.Detect(ctx)
if mode == mplatform.SystemGuest {
cancel()
return fmt.Errorf("nested virtualization not supported")
return errors.New("nested virtualization not supported")
} else if err != nil {
cancel()
return err
Expand All @@ -88,14 +88,14 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {
platform, ok = mplatform.PlatformsByName()[opts.platform]
if !ok {
cancel()
return fmt.Errorf("unknown platform driver: %s", opts.platform)
return errors.Errorf("unknown platform driver: %s", opts.platform)
}
}

strategy, ok := mplatform.Strategies()[platform]
if !ok {
cancel()
return fmt.Errorf("unsupported platform driver: %s (contributions welcome!)", platform.String())
return errors.Errorf("unsupported platform driver: %s (contributions welcome!)", platform.String())
}

controller, err := strategy.NewMachineV1alpha1(ctx)
Expand All @@ -116,7 +116,7 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {
pidfile, err = os.OpenFile(config.G[config.KraftKit](ctx).EventsPidFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o666)
if err != nil {
cancel()
return fmt.Errorf("could not create pidfile: %v", err)
return errors.Annotate(err, "could not create pidfile")
}

defer func() {
Expand All @@ -130,12 +130,12 @@ func (opts *Events) Run(cmd *cobra.Command, args []string) error {

if _, err := pidfile.Write([]byte(fmt.Sprintf("%d", os.Getpid()))); err != nil {
cancel()
return fmt.Errorf("failed to write PID file: %w", err)
return errors.Annotate(err, "failed to write PID file")
}

if err := pidfile.Sync(); err != nil {
cancel()
return fmt.Errorf("could not sync pid file: %v", err)
return errors.Annotate(err, "could not sync pid file")
}
}

Expand Down Expand Up @@ -165,7 +165,7 @@ seek:

machines, err := controller.List(ctx, &machineapi.MachineList{})
if err != nil {
return fmt.Errorf("could not list machines: %v", err)
return errors.Annotate(err, "could not list machines")
}

for _, machine := range machines.Items {
Expand Down
4 changes: 2 additions & 2 deletions cmd/kraft/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package fetch

import (
"fmt"
"os"

"github.com/MakeNowJust/heredoc"
"github.com/juju/errors"
"github.com/spf13/cobra"

"kraftkit.sh/cmdfactory"
Expand Down Expand Up @@ -109,7 +109,7 @@ func (opts *Fetch) Run(cmd *cobra.Command, args []string) error {
t = targets[0]

case config.G[config.KraftKit](ctx).NoPrompt:
return fmt.Errorf("could not determine which target to prepare")
return errors.New("could not determine which target to prepare")

default:
t, err = cli.SelectTarget(targets)
Expand Down
3 changes: 2 additions & 1 deletion cmd/kraft/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"kraftkit.sh/config"
"kraftkit.sh/iostreams"

"github.com/juju/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func (opts *Login) Run(cmd *cobra.Command, args []string) error {

btoken, err := term.ReadPassword(int(iostreams.G(ctx).In.Fd()))
if err != nil {
return fmt.Errorf("could not read password: %v", err)
return errors.Annotate(err, "could not read password")
}

fmt.Fprint(iostreams.G(ctx).Out, "\n")
Expand Down
Loading