error.go 988 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package http
  5. import (
  6. "fmt"
  7. "strconv"
  8. )
  9. // Error represent error response from InfluxDBServer or http error
  10. type Error struct {
  11. StatusCode int
  12. Code string
  13. Message string
  14. Err error
  15. RetryAfter uint
  16. }
  17. // Error fulfils error interface
  18. func (e *Error) Error() string {
  19. switch {
  20. case e.Err != nil:
  21. return e.Err.Error()
  22. case e.Code != "" && e.Message != "":
  23. return fmt.Sprintf("%s: %s", e.Code, e.Message)
  24. default:
  25. return "Unexpected status code " + strconv.Itoa(e.StatusCode)
  26. }
  27. }
  28. func (e *Error) Unwrap() error {
  29. if e.Err != nil {
  30. return e.Err
  31. }
  32. return nil
  33. }
  34. // NewError returns newly created Error initialised with nested error and default values
  35. func NewError(err error) *Error {
  36. return &Error{
  37. StatusCode: 0,
  38. Code: "",
  39. Message: "",
  40. Err: err,
  41. RetryAfter: 0,
  42. }
  43. }