chiark / gitweb /
rework InternalLogicError
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 11 Feb 2021 00:07:12 +0000 (00:07 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 12 Feb 2021 01:30:07 +0000 (01:30 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/error.rs
src/shapelib.rs

index f5b8cbaa64bec9413eb79a5aedd7c222a2b95fee..0a886b096faf6b0ea63ebb06457a1b8b7ad18c42 100644 (file)
@@ -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<backtrace::Backtrace>,
+}
+
+pub fn internal_logic_error<S: Into<Cow<'static, str>>>(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);
index 40cc898d1211edbfcc067665b3234ed4fc9aea91..c2e573640052c25fd311026fb5513f836d3ca9a6 100644 (file)
@@ -84,7 +84,7 @@ pub enum LibraryLoadError {
 
 impl LibraryLoadError {
   fn ought(self) -> InternalError {
-    InternalError::InternalLogicError(format!("{:?}", self))
+    internal_logic_error(format!("{:?}", self))
   }
 }