adam_web_ui/graph/
failure.rs1use crate::{Renderer, RuntimeFailure};
5
6#[derive(Debug)]
15pub enum GraphFailure {
16 InvalidInput(String),
18 Transport(String),
20 Runtime(Box<RuntimeFailure>),
22 Unavailable,
24}
25
26impl GraphFailure {
27 pub(crate) fn stops_channel(&self) -> bool {
29 matches!(self, Self::Runtime(_) | Self::Unavailable)
30 }
31}
32
33impl std::fmt::Display for GraphFailure {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 match self {
39 Self::InvalidInput(message) | Self::Transport(message) => f.write_str(message),
40 Self::Runtime(failure) => {
41 let source = failure.diagnostics.anonymous_source();
42 f.write_str(&crate::labels::format_adam_error(
43 &failure.error,
44 &failure.diagnostics,
45 source.map_or("", adam_lang::SourceId::text),
46 &failure.file_name,
47 &Renderer::plain(),
48 ))
49 }
50 Self::Unavailable => f.write_str("graph model unavailable after a mutating failure"),
51 }
52 }
53}
54
55impl std::error::Error for GraphFailure {}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
63 fn terminal_classification_preserves_runtime_cause() {
64 assert!(!GraphFailure::InvalidInput("bad bounds".into()).stops_channel());
65 assert!(!GraphFailure::Transport("bad wire".into()).stops_channel());
66 assert!(GraphFailure::Unavailable.stops_channel());
67 let error = crate::build_sheet(
68 "sheet s { cell divisor: i32 = 0; out result: i32 := 10 / divisor; }",
69 "graph-failure.adm2",
70 )
71 .err()
72 .unwrap();
73 let crate::BuildError::Runtime(failure) = error else {
74 panic!("runtime fixture");
75 };
76 assert!(GraphFailure::Runtime(failure).stops_channel());
77 }
78}