mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
[Crate] Separate error code into different files.
This commit is contained in:
parent
edbb0e6cf5
commit
995ab23b51
4 changed files with 115 additions and 90 deletions
71
src/errors/kind.rs
Normal file
71
src/errors/kind.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use crate::GDError;
|
||||
use std::error::Error;
|
||||
|
||||
/// All GameDig Error kinds.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum GDErrorKind {
|
||||
/// The received packet was bigger than the buffer size.
|
||||
PacketOverflow,
|
||||
/// The received packet was shorter than the expected one.
|
||||
PacketUnderflow,
|
||||
/// The received packet is badly formatted.
|
||||
PacketBad,
|
||||
/// Couldn't send the packet.
|
||||
PacketSend,
|
||||
/// Couldn't send the receive.
|
||||
PacketReceive,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl GDErrorKind {
|
||||
/// Convert error kind into a full error with a source (and implicit
|
||||
/// backtrace)
|
||||
///
|
||||
/// ```
|
||||
/// use gamedig::{GDErrorKind, GDResult};
|
||||
/// let _: GDResult<u32> = "thing".parse().map_err(|e| GDErrorKind::TypeParse.context(e));
|
||||
/// ```
|
||||
pub fn context<E: Into<Box<dyn Error + 'static>>>(self, source: E) -> GDError { GDError::from_error(self, source) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Testing cloning the GDErrorKind type
|
||||
#[test]
|
||||
fn test_cloning() {
|
||||
let error = GDErrorKind::BadGame;
|
||||
let cloned_error = error.clone();
|
||||
assert_eq!(error, cloned_error);
|
||||
}
|
||||
|
||||
// test display GDError
|
||||
#[test]
|
||||
fn test_display() {
|
||||
let err = GDErrorKind::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"
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue