xmachines_runtime/
error.rs1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum LoadErrorKind {
6 Invalid,
8 Code,
10 UnknownKey,
12 Unsupported,
14 InvalidTarget,
16 MissingImplementation,
18 InvalidParams,
20 DuplicateId,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct LoadError {
30 pub kind: LoadErrorKind,
31 pub path: String,
32 pub message: String,
33}
34
35impl LoadError {
36 pub(crate) fn new(kind: LoadErrorKind, path: impl Into<String>, message: impl Into<String>) -> Self {
37 Self { kind, path: path.into(), message: message.into() }
38 }
39}
40
41impl fmt::Display for LoadError {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 write!(f, "{} at {}", self.message, self.path)
44 }
45}
46
47impl std::error::Error for LoadError {}
48
49pub(crate) enum CodeValue<'a> {
52 Str(&'a str),
53 Raw(&'a str),
54}
55
56pub(crate) fn xstate_error(code: u32, values: &[CodeValue<'_>]) -> String {
60 let mut message = format!("XState error E{code}");
61 if !values.is_empty() {
62 let values: Vec<String> = values
63 .iter()
64 .map(|v| match v {
65 CodeValue::Str(s) => serde_json::Value::String((*s).to_owned()).to_string(),
66 CodeValue::Raw(s) => (*s).to_owned(),
67 })
68 .collect();
69 message.push_str(&format!(" ({})", values.join(", ")));
70 }
71 message.push_str(&format!(". See https://github.com/statelyai/xstate/blob/next/docs/errors.md#e{code}"));
72 message
73}