Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #89 from carmstrong/add_handle_error
Browse files Browse the repository at this point in the history
refactor(errors): move error calls and output to error class
  • Loading branch information
yichengq committed Jun 25, 2014
2 parents 9db2977 + c58aa61 commit f65102a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ $ etcdctl rm /foo/bar
Delete an empty directory or a key-value pair

```
$ etcdctl rmdir /path/to/dir
$ etcdctl rmdir /path/to/dir
```

or
Expand Down
10 changes: 10 additions & 0 deletions command/error.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package command

import (
"fmt"
"os"
)

const (
SUCCESS = iota
MalformedEtcdctlArguments
FailedToConnectToHost
FailedToAuth
ErrorFromEtcd
)

func handleError(code int, err error) {
fmt.Fprintln(os.Stderr, "Error: ", err)
os.Exit(code)
}
7 changes: 3 additions & 4 deletions command/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -67,8 +68,7 @@ func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) {
// Sync cluster.
if sync {
if ok := client.SyncCluster(); !ok {
fmt.Println("Cannot sync with the cluster using peers", peers)
os.Exit(FailedToConnectToHost)
handleError(FailedToConnectToHost, errors.New("Cannot sync with the cluster using peers " + strings.Join(peers, ", ")))
}
}

Expand All @@ -88,8 +88,7 @@ func handlePrint(c *cli.Context, fn handlerFunc, pFn printFunc) {

// Print error and exit, if necessary.
if err != nil {
fmt.Println("Error:", err)
os.Exit(ErrorFromEtcd)
handleError(ErrorFromEtcd, err)
}

if resp != nil && pFn != nil {
Expand Down
7 changes: 2 additions & 5 deletions command/watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package command

import (
"errors"
"fmt"
"os"
"os/signal"

Expand Down Expand Up @@ -63,8 +62,7 @@ func watchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, erro
case resp := <-receiver:
printAll(resp, c.GlobalString("output"))
case err := <-errCh:
fmt.Println("Error:", err)
os.Exit(-1)
handleError(-1, err)
}
}

Expand All @@ -74,8 +72,7 @@ func watchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, erro
resp, err = client.Watch(key, uint64(index), recursive, nil, nil)

if err != nil {
fmt.Println("Error:", err)
os.Exit(ErrorFromEtcd)
handleError(ErrorFromEtcd, err)
}

if err != nil {
Expand Down

0 comments on commit f65102a

Please sign in to comment.