Skip to main content

xript_runtime/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum XriptError {
5    #[error("invalid xript manifest:\n{}", format_issues(.issues))]
6    ManifestValidation { issues: Vec<ValidationIssue> },
7
8    #[error("binding error in `{binding}`: {message}")]
9    Binding { binding: String, message: String },
10
11    #[error("`{binding}()` requires the \"{capability}\" capability, which hasn't been granted to this script")]
12    CapabilityDenied { binding: String, capability: String },
13
14    #[error("execution limit exceeded: {limit}")]
15    ExecutionLimit { limit: String },
16
17    #[error("script error: {0}")]
18    Script(String),
19
20    #[error("QuickJS error: {0}")]
21    Engine(String),
22
23    #[error("JSON error: {0}")]
24    Json(#[from] serde_json::Error),
25
26    #[error("IO error: {0}")]
27    Io(#[from] std::io::Error),
28}
29
30#[derive(Debug, Clone)]
31pub struct ValidationIssue {
32    pub path: String,
33    pub message: String,
34}
35
36fn format_issues(issues: &[ValidationIssue]) -> String {
37    issues
38        .iter()
39        .map(|i| format!("  {}: {}", i.path, i.message))
40        .collect::<Vec<_>>()
41        .join("\n")
42}
43
44pub type Result<T> = std::result::Result<T, XriptError>;