error.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //! # Redis Error
  2. //!
  3. //! All redis errors are abstracted in this mod.
  4. use crate::value::Value;
  5. use thiserror::Error;
  6. /// Redis errors
  7. #[derive(Debug, PartialEq, Error)]
  8. pub enum Error {
  9. /// IO Error
  10. #[error("IO error {0}")]
  11. Io(String),
  12. /// Config
  13. #[error("Config error {0}")]
  14. Config(#[from] redis_config_parser::de::Error),
  15. /// Empty line
  16. #[error("No command provided")]
  17. EmptyLine,
  18. /// A command is not found
  19. #[error("unknown command `{0}`")]
  20. CommandNotFound(String),
  21. /// A sub-command is not found
  22. #[error("Unknown subcommand or wrong number of arguments for '{0}'. Try {1} HELP.")]
  23. SubCommandNotFound(String, String),
  24. /// Invalid number of arguments
  25. #[error("wrong number of arguments for '{0}' command")]
  26. InvalidArgsCount(String),
  27. /// Invalid Rank value
  28. #[error("{0} can't be zero: use 1 to start from the first match, 2 from the second, ...")]
  29. InvalidRank(String),
  30. /// The glob-pattern is not valid
  31. #[error("'{0}' is not a valid pattern")]
  32. InvalidPattern(String),
  33. /// Internal Error
  34. #[error("internal error")]
  35. Internal,
  36. /// Protocol error
  37. #[error("Protocol error: expected '{1}', got '{0}'")]
  38. Protocol(String, String),
  39. /// Unexpected argument
  40. #[error("Unknown subcommand or wrong number of arguments for '{1}'. Try {0} HELP.")]
  41. WrongArgument(String, String),
  42. /// We cannot incr by infinity
  43. #[error("increment would produce NaN or Infinity")]
  44. IncrByInfOrNan,
  45. /// Wrong number of arguments
  46. #[error("wrong number of arguments for '{0}' command")]
  47. WrongNumberArgument(String),
  48. /// Key not found
  49. #[error("no such key")]
  50. NotFound,
  51. /// Index out of range
  52. #[error("index out of range")]
  53. OutOfRange,
  54. /// String is bigger than max allowed size
  55. #[error("string exceeds maximum allowed size (proto-max-bulk-len)")]
  56. MaxAllowedSize,
  57. /// Attempting to move or copy to the same key
  58. #[error("source and destination objects are the same")]
  59. SameEntry,
  60. /// The connection is in pubsub only mode and the current command is not compatible.
  61. #[error("Can't execute '{0}': only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context")]
  62. PubsubOnly(String),
  63. /// Syntax error
  64. #[error("syntax error")]
  65. Syntax,
  66. /// Byte cannot be converted to a number
  67. #[error("value is not a valid number or out of range")]
  68. NotANumber,
  69. /// Not a number with specific number type
  70. #[error("value is not {0} or out of range")]
  71. NotANumberType(String),
  72. /// Number overflow
  73. #[error("increment or decrement would overflow")]
  74. Overflow,
  75. /// Unexpected negative number
  76. #[error("{0} is negative")]
  77. NegativeNumber(String),
  78. /// Invalid expire
  79. #[error("invalid expire time in {0}")]
  80. InvalidExpire(String),
  81. /// Invalid expiration options
  82. #[error("GT and LT options at the same time are not compatible")]
  83. InvalidExpireOpts,
  84. /// The connection is not in a transaction
  85. #[error("without MULTI")]
  86. NotInTx,
  87. /// Transaction was aborted
  88. #[error("Transaction discarded because of previous errors.")]
  89. TxAborted,
  90. /// The requested database does not exists
  91. #[error("DB index is out of range")]
  92. NotSuchDatabase,
  93. /// The connection is in a transaction and nested transactions are not supported
  94. #[error("calls can not be nested")]
  95. NestedTx,
  96. /// Watch is not allowed after a Multi has been called
  97. #[error("WATCH inside MULTI is not allowed")]
  98. WatchInsideTx,
  99. /// Wrong data type
  100. #[error("Operation against a key holding the wrong kind of value")]
  101. WrongType,
  102. /// Cursor error
  103. #[error("Error while creating or parsing the cursor: {0}")]
  104. Cursor(#[from] crate::value::cursor::Error),
  105. /// The connection has been unblocked by another connection and it wants to signal it
  106. /// through an error.
  107. #[error("client unblocked via CLIENT UNBLOCK")]
  108. UnblockByError,
  109. /// Options provided are not compatible
  110. #[error("{0} options at the same time are not compatible")]
  111. OptsNotCompatible(String),
  112. /// Unsupported option
  113. #[error("Unsupported option {0}")]
  114. UnsupportedOption(String),
  115. /// Client manual disconnection
  116. #[error("Manual disconnection")]
  117. Quit,
  118. }
  119. impl From<std::io::Error> for Error {
  120. fn from(err: std::io::Error) -> Self {
  121. Self::Io(err.to_string())
  122. }
  123. }
  124. impl From<Error> for Value {
  125. fn from(value: Error) -> Value {
  126. let err_type = match value {
  127. Error::WrongType => "WRONGTYPE",
  128. Error::NestedTx => "ERR MULTI",
  129. Error::NotInTx => "ERR EXEC",
  130. Error::TxAborted => "EXECABORT",
  131. Error::UnblockByError => "UNBLOCKED",
  132. _ => "ERR",
  133. };
  134. Value::Err(err_type.to_string(), value.to_string())
  135. }
  136. }