Skip to content

Instantly share code, notes, and snippets.

@brannon
Created June 9, 2014 22:46
Show Gist options
  • Save brannon/771ffb94311eba8347ff to your computer and use it in GitHub Desktop.
Save brannon/771ffb94311eba8347ff to your computer and use it in GitHub Desktop.
Dump error info in Go
// imports: strings, reflect, syscall
func dumpError(err error, depth int) {
indent := strings.Repeat(" ", depth*2)
errorType := reflect.TypeOf(err)
errorValue := reflect.ValueOf(err)
if errorType.Kind() == reflect.Ptr {
errorType = errorType.Elem()
errorValue = errorValue.Elem()
}
log.Printf("%sGot error: '%s'\n", indent, err.Error())
log.Printf("%s==> Error Type: '%v'\n", indent, errorType)
var errno syscall.Errno
if errorType.Kind() == reflect.Struct {
log.Printf("%s==> Error Values:\n", indent)
for i := 0; i < errorType.NumField(); i++ {
field := errorType.Field(i)
value := errorValue.Field(i)
log.Printf("%s==> %s: %v", indent, field.Name, value.String())
if nestedError, ok := value.Interface().(error); ok {
s.dumpError(nestedError, depth+1)
}
}
} else if errorType == reflect.TypeOf(errno) {
errno = errorValue.Interface().(syscall.Errno)
log.Printf("%s==> Errno: %d\n", indent, errno)
log.Printf("%s==> Message: %s\n", indent, errno)
} else {
log.Printf("%s==> Error Value:\n%s%v\n", indent, indent, errorValue.Interface())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment