1
0

error.rs 2.8 KB

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