Skip to content

Commit

Permalink
cmd/swarm: fix error handling in 'swarm up' (ethereum#14557)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
lmars authored and fjl committed Jun 6, 2017
1 parent 65ea913 commit 1e9f86b
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions cmd/swarm/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package main

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 1e9f86b

Please sign in to comment.