diff options
author | Lewis Marshall <lewis@lmars.net> | 2017-06-06 15:39:10 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-06-06 15:39:10 +0800 |
commit | 1e9f86b49e523b9e0867abf1499de8fca4c7cd8d (patch) | |
tree | 5882161df22d7b56f8952e130df628e0d6d0c6ad /cmd/swarm | |
parent | 65ea913e29c2945d6af4f05d9f61db5fbd0dd126 (diff) | |
download | go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.tar go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.tar.gz go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.tar.bz2 go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.tar.lz go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.tar.xz go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.tar.zst go-tangerine-1e9f86b49e523b9e0867abf1499de8fca4c7cd8d.zip |
cmd/swarm: fix error handling in 'swarm up' (#14557)
The error returned by client.Upload was previously being ignored due to becoming
out of scope outside the if statement. This has been fixed by instead defining a
function which returns the hash and error (rather than trying to set the hash in
each branch of the if statement).
Diffstat (limited to 'cmd/swarm')
-rw-r--r-- | cmd/swarm/upload.go | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/cmd/swarm/upload.go b/cmd/swarm/upload.go index 42673ae21..9f4c525bb 100644 --- a/cmd/swarm/upload.go +++ b/cmd/swarm/upload.go @@ -18,6 +18,7 @@ package main import ( + "errors" "fmt" "io" "io/ioutil" @@ -87,24 +88,32 @@ func upload(ctx *cli.Context) { if err != nil { utils.Fatalf("Error opening file: %s", err) } - var hash string + + // define a function which either uploads a directory or single file + // based on the type of the file being uploaded + var doUpload func() (hash string, err error) if stat.IsDir() { - if !recursive { - utils.Fatalf("Argument is a directory and recursive upload is disabled") + doUpload = func() (string, error) { + if !recursive { + return "", errors.New("Argument is a directory and recursive upload is disabled") + } + return client.UploadDirectory(file, defaultPath, "") } - hash, err = client.UploadDirectory(file, defaultPath, "") } else { - if mimeType == "" { - mimeType = detectMimeType(file) - } - f, err := swarm.Open(file) - if err != nil { - utils.Fatalf("Error opening file: %s", err) + doUpload = func() (string, error) { + f, err := swarm.Open(file) + if err != nil { + return "", fmt.Errorf("error opening file: %s", err) + } + defer f.Close() + if mimeType == "" { + mimeType = detectMimeType(file) + } + f.ContentType = mimeType + return client.Upload(f, "") } - defer f.Close() - f.ContentType = mimeType - hash, err = client.Upload(f, "") } + hash, err := doUpload() if err != nil { utils.Fatalf("Upload failed: %s", err) } |