From: Ian Jackson Date: Thu, 11 Feb 2021 00:07:12 +0000 (+0000) Subject: rework InternalLogicError X-Git-Tag: otter-0.4.0~555 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f2cfb0cbe90b63d3121becf24ab9483bebc82414;p=otter.git rework InternalLogicError Signed-off-by: Ian Jackson --- diff --git a/src/error.rs b/src/error.rs index f5b8cbaa..0a886b09 100644 --- a/src/error.rs +++ b/src/error.rs @@ -40,7 +40,7 @@ pub enum InternalError { #[error("Server MessagePack decoding error (game load failed) {0}")] MessagePackDecodeFail(#[from] rmp_serde::decode::Error), #[error("Server internal logic error {0}")] - InternalLogicError(String), + InternalLogicError(InternalLogicError), #[error("SVG processing/generation error {0:?}")] SVGProcessingFailed(#[from] SVGProcessingError), #[error("String formatting error {0}")] @@ -53,6 +53,30 @@ pub enum InternalError { PartialPlayerData, } +#[derive(Error)] +pub struct InternalLogicError { + desc: Cow<'static, str>, + backtrace: parking_lot::Mutex, +} + +pub fn internal_logic_error>>(desc: S) -> IE { + let backtrace = backtrace::Backtrace::new_unresolved(); + IE::InternalLogicError(InternalLogicError { + desc: desc.into(), + backtrace: parking_lot::Mutex::new(backtrace), + }) +} + +impl Debug for InternalLogicError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut backtrace = self.backtrace.lock(); + backtrace.resolve(); + write!(f, "internal logic error! {}\nbacktrace: {:?}", + self.desc, &backtrace) + } +} +display_as_debug!(InternalLogicError); + #[derive(Clone,Error,Debug,Serialize,Deserialize)] #[error("{0}")] pub struct TokenDeliveryError(String); diff --git a/src/shapelib.rs b/src/shapelib.rs index 40cc898d..c2e57364 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -84,7 +84,7 @@ pub enum LibraryLoadError { impl LibraryLoadError { fn ought(self) -> InternalError { - InternalError::InternalLogicError(format!("{:?}", self)) + internal_logic_error(format!("{:?}", self)) } }