mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
Separate errors into groups
This commit is contained in:
parent
3db40649f5
commit
f11587f2eb
2 changed files with 46 additions and 26 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::GDErrorKind;
|
use crate::{GDErrorKind, Packet, Socket};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
use std::{backtrace, fmt};
|
use std::{backtrace, fmt};
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,59 @@
|
||||||
use crate::GDError;
|
use crate::GDError;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
/// GameDig Error.
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum GDErrorKind {
|
pub enum Packet {
|
||||||
/// The received packet was bigger than the buffer size.
|
/// The received packet was bigger than the buffer size.
|
||||||
PacketOverflow,
|
Overflow,
|
||||||
/// The received packet was shorter than the expected one.
|
/// The received packet was shorter than the expected one.
|
||||||
PacketUnderflow,
|
Underflow,
|
||||||
/// The received packet is badly formatted.
|
/// The received packet is badly formatted.
|
||||||
PacketBad,
|
Bad,
|
||||||
/// Couldn't send the packet.
|
}
|
||||||
PacketSend,
|
|
||||||
/// Couldn't send the receive.
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
PacketReceive,
|
pub enum Parse {
|
||||||
/// Couldn't decompress data.
|
/// Couldn't decompress data.
|
||||||
Decompress,
|
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.
|
/// Couldn't cast a value to an enum.
|
||||||
UnknownEnumCast,
|
UnknownEnumCast,
|
||||||
/// Couldn't parse a json string.
|
/// Couldn't parse a json string.
|
||||||
JsonParse,
|
JsonParse,
|
||||||
/// Couldn't parse a value.
|
/// Couldn't parse a value.
|
||||||
TypeParse,
|
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 {
|
impl GDErrorKind {
|
||||||
|
|
@ -41,8 +61,8 @@ impl GDErrorKind {
|
||||||
/// backtrace)
|
/// backtrace)
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use gamedig::{GDErrorKind, GDResult};
|
/// use gamedig::{GDErrorKind, GDResult, Parse};
|
||||||
/// let _: GDResult<u32> = "thing".parse().map_err(|e| GDErrorKind::TypeParse.context(e));
|
/// 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) }
|
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
|
// Testing cloning the GDErrorKind type
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cloning() {
|
fn test_cloning() {
|
||||||
let error = GDErrorKind::BadGame;
|
let error = GDErrorKind::Parse(Parse::BadGame);
|
||||||
let cloned_error = error.clone();
|
let cloned_error = error.clone();
|
||||||
assert_eq!(error, cloned_error);
|
assert_eq!(error, cloned_error);
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +82,7 @@ mod tests {
|
||||||
// test display GDError
|
// test display GDError
|
||||||
#[test]
|
#[test]
|
||||||
fn test_display() {
|
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!(
|
assert_eq!(
|
||||||
format!("{err}"),
|
format!("{err}"),
|
||||||
"GDError{ kind=BadGame\n source=\"Rust is not a game\"\n backtrace=<disabled>\n}\n"
|
"GDError{ kind=BadGame\n source=\"Rust is not a game\"\n backtrace=<disabled>\n}\n"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue