Skip to content

Commit

Permalink
Merge pull request #135 from buildpack/better-errors
Browse files Browse the repository at this point in the history
Use error message/code helpers correctly
  • Loading branch information
ekcasey authored May 14, 2019
2 parents 8612386 + f42cb60 commit a5743d7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 46 deletions.
25 changes: 12 additions & 13 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -42,10 +44,13 @@ func main() {
log.SetOutput(ioutil.Discard)

flag.Parse()
repoName = flag.Arg(0)
if flag.NArg() > 1 || repoName == "" {
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "parse arguments"))
if flag.NArg() > 1 {
cmd.Exit(cmd.FailErrCode(fmt.Errorf("received %d args expected 1", flag.NArg()), cmd.CodeInvalidArgs, "parse arguments"))
}
if flag.Arg(0) == "" {
cmd.Exit(cmd.FailErrCode(errors.New("image argument is required"), cmd.CodeInvalidArgs, "parse arguments"))
}
repoName = flag.Arg(0)
cmd.Exit(analyzer())
}

Expand Down Expand Up @@ -77,27 +82,21 @@ func analyzer() error {
if useDaemon {
dockerClient, err := docker.DefaultClient()
if err != nil {
return err
return cmd.FailErr(err, "create docker client")
}
previousImage, err = imgutil.NewLocalImage(repoName, dockerClient)
if err != nil {
return err
return cmd.FailErr(err, "access previous image")
}
} else {
previousImage, err = imgutil.NewRemoteImage(repoName, auth.DefaultEnvKeychain())
if err != nil {
return err
}
}
if err != nil {
return cmd.FailErr(err, "repository configuration", repoName)
}

err = analyzer.Analyze(
previousImage,
)
if err != nil {
return cmd.FailErrCode(err, cmd.CodeFailedBuild)
if err := analyzer.Analyze(previousImage); err != nil {
return cmd.FailErrCode(err, cmd.CodeFailed, "analyze")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func build() error {

metadata, err := builder.Build()
if err != nil {
return cmd.FailErrCode(err, cmd.CodeFailedBuild)
return cmd.FailErrCode(err, cmd.CodeFailedBuild, "build")
}

metadataPath := filepath.Join(layersDir, "config", "metadata.toml")
Expand Down
15 changes: 7 additions & 8 deletions cmd/cacher/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -40,11 +40,10 @@ func main() {

flag.Parse()
if flag.NArg() > 0 {
args := map[string]interface{}{"narg": flag.NArg(), "layersDir": layersDir}
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "parse arguments", fmt.Sprintf("%+v", args)))
cmd.Exit(cmd.FailErrCode(errors.New("received unexpected args"), cmd.CodeInvalidArgs, "parse arguments"))
}
if cacheImageTag == "" && cacheDir == "" {
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "must supply either -image or -path"))
cmd.Exit(cmd.FailErrCode(errors.New("must supply either -image or -path"), cmd.CodeInvalidArgs, "parse arguments"))
}
cmd.Exit(doCache())
}
Expand Down Expand Up @@ -74,12 +73,12 @@ func doCache() error {
if cacheImageTag != "" {
dockerClient, err := docker.DefaultClient()
if err != nil {
return err
return cmd.FailErr(err, "create docker client")
}

origCacheImage, err := imgutil.NewLocalImage(cacheImageTag, dockerClient)
if err != nil {
return err
return cmd.FailErr(err, "access cache image")
}

cacheStore = cache.NewImageCache(
Expand All @@ -90,12 +89,12 @@ func doCache() error {
var err error
cacheStore, err = cache.NewVolumeCache(cacheDir)
if err != nil {
return err
return cmd.FailErr(err, "create volume cache")
}
}

if err := cacher.Cache(layersDir, cacheStore); err != nil {
return cmd.FailErrCode(err, cmd.CodeFailed)
return cmd.FailErrCode(err, cmd.CodeFailed, "cache")
}

return nil
Expand Down
33 changes: 17 additions & 16 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -53,12 +55,15 @@ func main() {
log.SetOutput(ioutil.Discard)

flag.Parse()
if flag.NArg() > 1 || flag.Arg(0) == "" {
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "parse arguments"))
if flag.NArg() > 1 {
cmd.Exit(cmd.FailErrCode(fmt.Errorf("received %d args expected 1", flag.NArg()), cmd.CodeInvalidArgs, "parse arguments"))
}
if flag.Arg(0) == "" {
cmd.Exit(cmd.FailErrCode(errors.New("image argument is required"), cmd.CodeInvalidArgs, "parse arguments"))
}

if launchCacheDir != "" && !useDaemon {
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "launch cache can only be used when exporting to a Docker daemon"))
cmd.Exit(cmd.FailErrCode(errors.New("launch cache can only be used when exporting to a Docker daemon"), cmd.CodeInvalidArgs, "parse arguments"))
}

repoName = flag.Arg(0)
Expand Down Expand Up @@ -98,7 +103,7 @@ func export() error {

if runImageRef == "" {
if stack.RunImage.Image == "" {
return cmd.FailCode(cmd.CodeInvalidArgs, "-image is required when there is no stack metadata available")
return cmd.FailErrCode(errors.New("-image is required when there is no stack metadata available"), cmd.CodeInvalidArgs, "parse arguments")
}

runImageRef, err = runImageFromStackToml(stack)
Expand All @@ -122,54 +127,50 @@ func export() error {

runImage, err = imgutil.NewLocalImage(runImageRef, dockerClient)
if err != nil {
return err
return cmd.FailErr(err, "access run image")
}

if launchCacheDir != "" {
volumeCache, err := cache.NewVolumeCache(launchCacheDir)
if err != nil {
return err
return cmd.FailErr(err, "create launch cache")
}
runImage = lifecycle.NewCachingImage(runImage, volumeCache)
}

origImage, err = imgutil.NewLocalImage(repoName, dockerClient)
if err != nil {
return err
return cmd.FailErr(err, "access previous image")
}
} else {
runImage, err = imgutil.NewRemoteImage(runImageRef, auth.DefaultEnvKeychain())
if err != nil {
return err
return cmd.FailErr(err, "access run image")
}
origImage, err = imgutil.NewRemoteImage(repoName, auth.DefaultEnvKeychain())
if err != nil {
return err
return cmd.FailErr(err, "access previous image")
}
}

if err := exporter.Export(layersDir, appDir, runImage, origImage, launcherPath, stack); err != nil {
return cmd.FailErrCode(err, cmd.CodeFailedBuild)
return cmd.FailErr(err, "export")
}

return nil
}

func runImageFromStackToml(stack metadata.StackMetadata) (string, error) {
if stack.RunImage.Image == "" {
return "", cmd.FailCode(cmd.CodeInvalidArgs, "-image is required when there is no stack metadata available")
}

registry, err := image.ParseRegistry(repoName)
if err != nil {
return "", cmd.FailCode(cmd.CodeInvalidArgs, "parse image name", err.Error())
return "", cmd.FailErrCode(err, cmd.CodeInvalidArgs, "parse image name")
}

runImageMirrors := []string{stack.RunImage.Image}
runImageMirrors = append(runImageMirrors, stack.RunImage.Mirrors...)
runImageRef, err := image.ByRegistry(registry, runImageMirrors)
if err != nil {
return "", cmd.FailCode(cmd.CodeInvalidArgs, "parse mirrors", err.Error())
return "", cmd.FailErrCode(err, cmd.CodeInvalidArgs, "parse mirrors")
}
return runImageRef, nil
}
15 changes: 7 additions & 8 deletions cmd/restorer/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -40,11 +40,10 @@ func main() {

flag.Parse()
if flag.NArg() > 0 {
args := map[string]interface{}{"narg": flag.NArg(), "layersDir": layersDir}
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "parse arguments", fmt.Sprintf("%+v", args)))
cmd.Exit(cmd.FailErrCode(errors.New("received unexpected args"), cmd.CodeInvalidArgs, "parse arguments"))
}
if cacheImageTag == "" && cacheDir == "" {
cmd.Exit(cmd.FailCode(cmd.CodeInvalidArgs, "must supply either -image or -path"))
cmd.Exit(cmd.FailErrCode(errors.New("must supply either -image or -path"), cmd.CodeInvalidArgs, "parse arguments"))
}
cmd.Exit(restore())
}
Expand All @@ -68,12 +67,12 @@ func restore() error {
if cacheImageTag != "" {
dockerClient, err := docker.DefaultClient()
if err != nil {
return err
return cmd.FailErr(err, "create docker client")
}

cacheImage, err := imgutil.NewLocalImage(cacheImageTag, dockerClient)
if err != nil {
return err
return cmd.FailErr(err, "access cache image")
}

cacheStore = cache.NewImageCache(
Expand All @@ -84,12 +83,12 @@ func restore() error {
var err error
cacheStore, err = cache.NewVolumeCache(cacheDir)
if err != nil {
return err
return cmd.FailErr(err, "create volume cache")
}
}

if err := restorer.Restore(cacheStore); err != nil {
return cmd.FailErrCode(err, cmd.CodeFailed)
return cmd.FailErrCode(err, cmd.CodeFailed, "restore")
}
return nil
}

0 comments on commit a5743d7

Please sign in to comment.