-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from Bryntet/pumpkinerror-trait
Add a general error trait for simplified use of Pumpkin errors
- Loading branch information
Showing
4 changed files
with
81 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use log::log; | ||
use pumpkin_inventory::InventoryError; | ||
use pumpkin_protocol::bytebuf::DeserializerError; | ||
use std::fmt::Display; | ||
|
||
pub trait PumpkinError: std::error::Error + Display { | ||
fn is_kick(&self) -> bool; | ||
|
||
fn log(&self) { | ||
log!(self.severity(), "{}", self.to_string()); | ||
} | ||
|
||
fn severity(&self) -> log::Level; | ||
|
||
fn client_kick_reason(&self) -> Option<String>; | ||
} | ||
|
||
impl<ErrorType: PumpkinError + 'static> From<ErrorType> for Box<dyn PumpkinError> { | ||
fn from(error: ErrorType) -> Self { | ||
Box::new(error) | ||
} | ||
} | ||
impl PumpkinError for InventoryError { | ||
fn is_kick(&self) -> bool { | ||
use InventoryError::*; | ||
match self { | ||
InvalidSlot | ClosedContainerInteract(..) | InvalidPacket | PermissionError => true, | ||
LockError | OutOfOrderDragging | MultiplePlayersDragging => false, | ||
} | ||
} | ||
fn severity(&self) -> log::Level { | ||
use InventoryError::*; | ||
match self { | ||
LockError | ||
| InvalidSlot | ||
| ClosedContainerInteract(..) | ||
| InvalidPacket | ||
| PermissionError => log::Level::Error, | ||
OutOfOrderDragging => log::Level::Info, | ||
MultiplePlayersDragging => log::Level::Warn, | ||
} | ||
} | ||
|
||
fn client_kick_reason(&self) -> Option<String> { | ||
None | ||
} | ||
} | ||
|
||
impl PumpkinError for DeserializerError { | ||
fn is_kick(&self) -> bool { | ||
true | ||
} | ||
|
||
fn severity(&self) -> log::Level { | ||
log::Level::Error | ||
} | ||
|
||
fn client_kick_reason(&self) -> Option<String> { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters