error.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //! # Redis Error
  2. //!
  3. //! All redis errors are abstracted in this mod.
  4. use crate::value::Value;
  5. /// Redis errors
  6. #[derive(Debug, Eq, PartialEq)]
  7. pub enum Error {
  8. /// A command is not found
  9. CommandNotFound(String),
  10. /// Invalid number of arguments
  11. InvalidArgsCount(String),
  12. /// The glob-pattern is not valid
  13. InvalidPattern(String),
  14. /// Internal Error
  15. Internal,
  16. /// Protocol error
  17. Protocol(String, String),
  18. /// Unexpected argument
  19. WrongArgument(String, String),
  20. /// Command not found
  21. NotFound,
  22. /// Index out of range
  23. OutOfRange,
  24. /// The connection is in pubsub only mode and the current command is not compabible.
  25. PubsubOnly(String),
  26. /// Syntax error
  27. Syntax,
  28. /// Byte cannot be converted to a number
  29. NotANumber,
  30. /// The connection is not in a transaction
  31. NotInTx,
  32. /// The connection is in a transaction and nested transactions are not supported
  33. NestedTx,
  34. /// Wrong data type
  35. WrongType,
  36. }
  37. impl From<Error> for Value {
  38. fn from(value: Error) -> Value {
  39. let err_type = match value {
  40. Error::WrongType => "WRONGTYPE",
  41. Error::NestedTx => "ERR MULTI",
  42. Error::NotInTx => "ERR EXEC",
  43. _ => "ERR",
  44. };
  45. let err_msg = match value {
  46. Error::CommandNotFound(x) => format!("unknown command `{}`", x),
  47. Error::InvalidArgsCount(x) => format!("wrong number of arguments for '{}' command", x),
  48. Error::InvalidPattern(x) => format!("'{}' is not a valid pattern", x),
  49. Error::Internal => "internal error".to_owned(),
  50. Error::Protocol(x, y) => format!("Protocol error: expected '{}', got '{}'", x, y),
  51. Error::NotInTx => " without MULTI".to_owned(),
  52. Error::NotANumber => "value is not an integer or out of range".to_owned(),
  53. Error::OutOfRange => "index out of range".to_owned(),
  54. Error::Syntax => "syntax error".to_owned(),
  55. Error::NotFound => "no such key".to_owned(),
  56. Error::NestedTx => "calls can not be nested".to_owned(),
  57. Error::PubsubOnly(x) => format!("Can't execute '{}': only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context", x),
  58. Error::WrongArgument(x, y) => format!(
  59. "Unknown subcommand or wrong number of arguments for '{}'. Try {} HELP.",
  60. y, x
  61. ),
  62. Error::WrongType => {
  63. "Operation against a key holding the wrong kind of value".to_owned()
  64. }
  65. };
  66. Value::Err(err_type.to_string(), err_msg)
  67. }
  68. }