1use std::io;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum Error {
6 #[error("IO error: {0}")]
7 Io(#[from] io::Error),
8
9 #[error("FeOxDB error: {0}")]
10 Database(#[from] feoxdb::FeoxError),
11
12 #[error("Protocol error: {0}")]
13 Protocol(String),
14
15 #[error("Connection closed")]
16 ConnectionClosed,
17
18 #[error("Buffer overflow")]
19 BufferOverflow,
20
21 #[error("Invalid command: {0}")]
22 InvalidCommand(String),
23
24 #[error("Wrong number of arguments for '{0}' command")]
25 WrongArity(String),
26
27 #[error("Configuration error: {0}")]
28 Config(String),
29
30 #[error("Server shutting down")]
31 Shutdown,
32
33 #[error("System error: {0}")]
34 System(String),
35
36 #[error(transparent)]
37 Other(#[from] anyhow::Error),
38}
39
40pub type Result<T> = std::result::Result<T, Error>;
41
42impl Error {
43 pub fn to_resp(&self) -> Vec<u8> {
45 match self {
46 Error::Database(feoxdb::FeoxError::KeyNotFound) => {
47 b"$-1\r\n".to_vec() }
49 Error::WrongArity(cmd) => {
50 format!("-ERR wrong number of arguments for '{}' command\r\n", cmd).into_bytes()
51 }
52 Error::InvalidCommand(cmd) => {
53 format!("-ERR unknown command '{}'\r\n", cmd).into_bytes()
54 }
55 _ => format!("-ERR {}\r\n", self).into_bytes(),
56 }
57 }
58}