error.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "flag"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. errorsutil "k8s.io/apimachinery/pkg/util/errors"
  21. )
  22. const (
  23. // DefaultErrorExitCode defines exit the code for failed action generally
  24. DefaultErrorExitCode = 1
  25. // PreFlightExitCode defines exit the code for preflight checks
  26. PreFlightExitCode = 2
  27. // ValidationExitCode defines the exit code validation checks
  28. ValidationExitCode = 3
  29. )
  30. // fatal prints the message if set and then exits.
  31. func fatal(msg string, code int) {
  32. if len(msg) > 0 {
  33. // add newline if needed
  34. if !strings.HasSuffix(msg, "\n") {
  35. msg += "\n"
  36. }
  37. fmt.Fprint(os.Stderr, msg)
  38. }
  39. os.Exit(code)
  40. }
  41. // CheckErr prints a user friendly error to STDERR and exits with a non-zero
  42. // exit code. Unrecognized errors will be printed with an "error: " prefix.
  43. //
  44. // This method is generic to the command in use and may be used by non-Kubectl
  45. // commands.
  46. func CheckErr(err error) {
  47. checkErr(err, fatal)
  48. }
  49. // preflightError allows us to know if the error is a preflight error or not
  50. // defining the interface here avoids an import cycle of pulling in preflight into the util package
  51. type preflightError interface {
  52. Preflight() bool
  53. }
  54. // checkErr formats a given error as a string and calls the passed handleErr
  55. // func with that string and an exit code.
  56. func checkErr(err error, handleErr func(string, int)) {
  57. var msg string
  58. if err != nil {
  59. msg = fmt.Sprintf("%s\nTo see the stack trace of this error execute with --v=5 or higher", err.Error())
  60. // check if the verbosity level in klog is high enough and print a stack trace.
  61. f := flag.CommandLine.Lookup("v")
  62. if f != nil {
  63. // assume that the "v" flag contains a parseable Int32 as per klog's "Level" type alias,
  64. // thus no error from ParseInt is handled here.
  65. if v, e := strconv.ParseInt(f.Value.String(), 10, 32); e == nil {
  66. // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md
  67. // klog.V(5) - Trace level verbosity
  68. if v > 4 {
  69. msg = fmt.Sprintf("%+v", err)
  70. }
  71. }
  72. }
  73. }
  74. switch err.(type) {
  75. case nil:
  76. return
  77. case preflightError:
  78. handleErr(msg, PreFlightExitCode)
  79. case errorsutil.Aggregate:
  80. handleErr(msg, ValidationExitCode)
  81. default:
  82. handleErr(msg, DefaultErrorExitCode)
  83. }
  84. }
  85. // FormatErrMsg returns a human-readable string describing the slice of errors passed to the function
  86. func FormatErrMsg(errs []error) string {
  87. var errMsg string
  88. for _, err := range errs {
  89. errMsg = fmt.Sprintf("%s\t- %s\n", errMsg, err.Error())
  90. }
  91. return errMsg
  92. }