errors.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package memcache
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. pkgerr "github.com/pkg/errors"
  7. )
  8. var (
  9. // ErrNotFound not found
  10. ErrNotFound = errors.New("memcache: key not found")
  11. // ErrExists exists
  12. ErrExists = errors.New("memcache: key exists")
  13. // ErrNotStored not stored
  14. ErrNotStored = errors.New("memcache: key not stored")
  15. // ErrCASConflict means that a CompareAndSwap call failed due to the
  16. // cached value being modified between the Get and the CompareAndSwap.
  17. // If the cached value was simply evicted rather than replaced,
  18. // ErrNotStored will be returned instead.
  19. ErrCASConflict = errors.New("memcache: compare-and-swap conflict")
  20. // ErrPoolExhausted is returned from a pool connection method (Store, Get,
  21. // Delete, IncrDecr, Err) when the maximum number of database connections
  22. // in the pool has been reached.
  23. ErrPoolExhausted = errors.New("memcache: connection pool exhausted")
  24. // ErrPoolClosed pool closed
  25. ErrPoolClosed = errors.New("memcache: connection pool closed")
  26. // ErrConnClosed conn closed
  27. ErrConnClosed = errors.New("memcache: connection closed")
  28. // ErrMalformedKey is returned when an invalid key is used.
  29. // Keys must be at maximum 250 bytes long and not
  30. // contain whitespace or control characters.
  31. ErrMalformedKey = errors.New("memcache: malformed key is too long or contains invalid characters")
  32. // ErrValueSize item value size must less than 1mb
  33. ErrValueSize = errors.New("memcache: item value size must not greater than 1mb")
  34. // ErrStat stat error for monitor
  35. ErrStat = errors.New("memcache unexpected errors")
  36. // ErrItem item nil.
  37. ErrItem = errors.New("memcache: item object nil")
  38. // ErrItemObject object type Assertion failed
  39. ErrItemObject = errors.New("memcache: item object protobuf type assertion failed")
  40. )
  41. type protocolError string
  42. func (pe protocolError) Error() string {
  43. return fmt.Sprintf("memcache: %s (possible server error or unsupported concurrent read by application)", string(pe))
  44. }
  45. func formatErr(err error) string {
  46. e := pkgerr.Cause(err)
  47. switch e {
  48. case ErrNotFound, ErrExists, ErrNotStored, nil:
  49. return ""
  50. default:
  51. es := e.Error()
  52. switch {
  53. case strings.HasPrefix(es, "read"):
  54. return "read timeout"
  55. case strings.HasPrefix(es, "dial"):
  56. return "dial timeout"
  57. case strings.HasPrefix(es, "write"):
  58. return "write timeout"
  59. case strings.Contains(es, "EOF"):
  60. return "eof"
  61. case strings.Contains(es, "reset"):
  62. return "reset"
  63. case strings.Contains(es, "broken"):
  64. return "broken pipe"
  65. default:
  66. return "unexpected err"
  67. }
  68. }
  69. }