error.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "os"
  19. "github.com/urfave/cli"
  20. "go.etcd.io/etcd/client"
  21. )
  22. const (
  23. ExitSuccess = iota
  24. ExitBadArgs
  25. ExitBadConnection
  26. ExitBadAuth
  27. ExitServerError
  28. ExitClusterNotHealthy
  29. )
  30. func handleError(c *cli.Context, code int, err error) {
  31. if c.GlobalString("output") == "json" {
  32. if err, ok := err.(*client.Error); ok {
  33. b, err := json.Marshal(err)
  34. if err != nil {
  35. panic(err)
  36. }
  37. fmt.Fprintln(os.Stderr, string(b))
  38. os.Exit(code)
  39. }
  40. }
  41. fmt.Fprintln(os.Stderr, "Error: ", err)
  42. if cerr, ok := err.(*client.ClusterError); ok {
  43. fmt.Fprintln(os.Stderr, cerr.Detail())
  44. }
  45. os.Exit(code)
  46. }