Skip to content

Instantly share code, notes, and snippets.

@travisjeffery
Created November 1, 2017 05:33
Show Gist options
  • Save travisjeffery/39ed88eea714efdb1a11f8b494a85784 to your computer and use it in GitHub Desktop.
Save travisjeffery/39ed88eea714efdb1a11f8b494a85784 to your computer and use it in GitHub Desktop.
// error.go
// WrapErr returns a corev1.Error for the given error and msg.
func WrapErr(err error, msg string) error {
if err == nil {
return nil
}
e := &Error{Message: fmt.Sprintf("%s: %s", msg, err.Error())}
e.populateStack()
return e
}
// E is a useful func for instantiating corev1.Errors.
func E(args ...interface{}) error {
if len(args) == 0 {
panic("call to E with no arguments")
}
e := &Error{}
b := new(bytes.Buffer)
for _, arg := range args {
switch arg := arg.(type) {
case string:
pad(b, ": ")
b.WriteString(arg)
case int32:
e.Code = arg
case int:
e.Code = int32(arg)
case error:
pad(b, ": ")
b.WriteString(arg.Error())
}
}
e.Message = b.String()
e.populateStack()
return e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment