1use std::{fmt, sync::Arc};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum AppErrorKind {
10 Load,
13 Uncaught,
16 Stopped,
20 HotUpdate,
23 Asset,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct PackageInfo {
31 pub id: String,
33 pub name: String,
35 pub version: String,
37 pub build: u64,
39 pub sha256: String,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq)]
47#[non_exhaustive]
48pub struct AppError {
49 pub kind: AppErrorKind,
51 pub message: String,
53 pub stack: Option<String>,
56 pub package: Option<Arc<PackageInfo>>,
58}
59
60impl AppError {
61 pub fn new(kind: AppErrorKind, text: &str) -> AppError {
64 let (message, stack) = split_stack(text);
65 AppError {
66 kind,
67 message: message.to_owned(),
68 stack: stack.map(str::to_owned),
69 package: None,
70 }
71 }
72
73 pub fn with_package(mut self, package: Option<Arc<PackageInfo>>) -> AppError {
75 self.package = package;
76 self
77 }
78
79 pub fn is_fatal(&self) -> bool {
83 matches!(self.kind, AppErrorKind::Load | AppErrorKind::Stopped)
84 }
85
86 pub fn is_stopped(&self) -> bool {
88 self.kind == AppErrorKind::Stopped
89 }
90}
91
92impl fmt::Display for AppError {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 f.write_str(&self.message)?;
95 if let Some(stack) = &self.stack {
96 write!(f, "\n{stack}")?;
97 }
98 Ok(())
99 }
100}
101
102impl std::error::Error for AppError {}
103
104fn split_stack(text: &str) -> (&str, Option<&str>) {
108 let mut start = 0;
109 for line in text.split_inclusive('\n') {
110 if start > 0 && line.trim_start().starts_with("at ") {
111 return (&text[..start - 1], Some(&text[start..]));
112 }
113 start += line.len();
114 }
115 (text, None)
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 #[test]
123 fn the_stack_is_split_from_the_message() {
124 let text = "Error: boom\n at fail (App.tsx:5:9)\n at <anonymous> (App.tsx:7:1)\n";
125 let error = AppError::new(AppErrorKind::Uncaught, text);
126 assert_eq!(error.message, "Error: boom");
127 assert_eq!(
128 error.stack.as_deref(),
129 Some(" at fail (App.tsx:5:9)\n at <anonymous> (App.tsx:7:1)\n")
130 );
131 assert_eq!(error.to_string(), text);
132 }
133
134 #[test]
135 fn messages_keep_their_lines_and_may_have_no_stack() {
136 let text = "Error: two\nlines\n at f (a.js:1:1)";
137 let error = AppError::new(AppErrorKind::Load, text);
138 assert_eq!(error.message, "Error: two\nlines");
139 assert_eq!(error.to_string(), text);
140 let plain = AppError::new(AppErrorKind::Stopped, "the app ran out of memory");
141 assert_eq!(plain.stack, None);
142 assert_eq!(plain.to_string(), "the app ran out of memory");
143 assert_eq!(AppError::new(AppErrorKind::Uncaught, "at once").stack, None);
145 }
146
147 #[test]
148 fn load_failures_and_stops_are_fatal() {
149 let kinds = [
150 (AppErrorKind::Load, true, false),
151 (AppErrorKind::Stopped, true, true),
152 (AppErrorKind::Uncaught, false, false),
153 (AppErrorKind::HotUpdate, false, false),
154 (AppErrorKind::Asset, false, false),
155 ];
156 for (kind, fatal, stopped) in kinds {
157 let error = AppError::new(kind, "x");
158 assert_eq!(
159 (error.is_fatal(), error.is_stopped()),
160 (fatal, stopped),
161 "{kind:?}"
162 );
163 }
164 }
165}