Separate errors into groups

This commit is contained in:
CosminPerRam 2023-08-26 02:58:19 +03:00
parent 3db40649f5
commit f11587f2eb
2 changed files with 46 additions and 26 deletions

View file

@ -1,4 +1,4 @@
use crate::GDErrorKind;
use crate::{GDErrorKind, Packet, Socket};
use std::error::Error;
use std::fmt::Formatter;
use std::{backtrace, fmt};

View file

@ -1,39 +1,59 @@
use crate::GDError;
use std::error::Error;
/// GameDig Error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GDErrorKind {
pub enum Packet {
/// The received packet was bigger than the buffer size.
PacketOverflow,
Overflow,
/// The received packet was shorter than the expected one.
PacketUnderflow,
Underflow,
/// The received packet is badly formatted.
PacketBad,
/// Couldn't send the packet.
PacketSend,
/// Couldn't send the receive.
PacketReceive,
Bad,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Parse {
/// Couldn't decompress data.
Decompress,
/// Couldn't create a socket connection.
SocketConnect,
/// Couldn't bind a socket.
SocketBind,
/// Invalid input.
InvalidInput,
/// The server queried is not the queried game server.
BadGame,
/// Couldn't automatically query.
AutoQuery,
/// A protocol-defined expected format was not met.
ProtocolFormat,
/// Couldn't cast a value to an enum.
UnknownEnumCast,
/// Couldn't parse a json string.
JsonParse,
/// Couldn't parse a value.
TypeParse,
/// A protocol-defined expected format was not met.
ProtocolFormat,
/// The server queried is not the queried game server.
BadGame,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Socket {
/// Couldn't create a socket connection.
Connect,
/// Couldn't bind a socket.
Bind,
/// Couldn't send a packet.
Send,
/// Couldn't receive a packet.
Receive,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Input {
/// Invalid input.
Invalid,
/// Couldn't automatically query.
AutoQuery,
}
/// GameDig Error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GDErrorKind {
Packet(Packet),
Parse(Parse),
Socket(Socket),
Input(Input),
}
impl GDErrorKind {
@ -41,8 +61,8 @@ impl GDErrorKind {
/// backtrace)
///
/// ```
/// use gamedig::{GDErrorKind, GDResult};
/// let _: GDResult<u32> = "thing".parse().map_err(|e| GDErrorKind::TypeParse.context(e));
/// use gamedig::{GDErrorKind, GDResult, Parse};
/// let _: GDResult<u32> = "thing".parse().map_err(|e| GDErrorKind::Parse(Parse::TypeParse).context(e));
/// ```
pub fn context<E: Into<Box<dyn Error + 'static>>>(self, source: E) -> GDError { GDError::from_error(self, source) }
}
@ -54,7 +74,7 @@ mod tests {
// Testing cloning the GDErrorKind type
#[test]
fn test_cloning() {
let error = GDErrorKind::BadGame;
let error = GDErrorKind::Parse(Parse::BadGame);
let cloned_error = error.clone();
assert_eq!(error, cloned_error);
}
@ -62,7 +82,7 @@ mod tests {
// test display GDError
#[test]
fn test_display() {
let err = GDErrorKind::BadGame.context("Rust is not a game");
let err = Parse::BadGame.context("Rust is not a game");
assert_eq!(
format!("{err}"),
"GDError{ kind=BadGame\n source=\"Rust is not a game\"\n backtrace=<disabled>\n}\n"