clickhouse_exception.go 866 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package clickhouse
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Exception struct {
  7. Code int32
  8. Name string
  9. Message string
  10. StackTrace string
  11. nested error
  12. }
  13. func (e *Exception) Error() string {
  14. return fmt.Sprintf("code: %d, message: %s", e.Code, e.Message)
  15. }
  16. func (ch *clickhouse) exception() error {
  17. var (
  18. e Exception
  19. err error
  20. hasNested bool
  21. )
  22. if e.Code, err = ch.decoder.Int32(); err != nil {
  23. return err
  24. }
  25. if e.Name, err = ch.decoder.String(); err != nil {
  26. return err
  27. }
  28. if e.Message, err = ch.decoder.String(); err != nil {
  29. return err
  30. }
  31. e.Message = strings.TrimSpace(strings.TrimPrefix(e.Message, e.Name+":"))
  32. if e.StackTrace, err = ch.decoder.String(); err != nil {
  33. return err
  34. }
  35. if hasNested, err = ch.decoder.Bool(); err != nil {
  36. return err
  37. }
  38. if hasNested {
  39. e.nested = ch.exception()
  40. }
  41. return &e
  42. }