use spanned_error_message::{
Location,
SpannedErrorMessage,
};
use xee_xpath::error::ErrorValue;
use crate::errors::{
BunsenError,
ParseError,
};
pub fn pretty_errorvalue(e: &ErrorValue) -> String {
format!("XPath Error: {}: {}\n{}", e.code(), e.message(), e.note())
}
fn offset_to_location(
src: &str,
offset: usize,
) -> Location {
let src = &src[..offset];
let line = src.lines().count();
let column = src.lines().last().unwrap().len();
Location { line, column }
}
fn error_kind_label(e: &ErrorValue) -> &'static str {
if matches!(e, ErrorValue::XPST0003) {
return "Parse Error";
}
if matches!(e, ErrorValue::Application(_)) {
return "XPath Error";
}
let code = e.code();
match code.get(..4) {
Some("XPST" | "XQST") => "Static Error",
Some("XPTY" | "XQTY") => "Type Error",
Some("XPDY" | "XQDY") => "Dynamic Error",
_ if code.starts_with("FO") => "Function Error",
_ => "XPath Error",
}
}
pub fn adapt_xee_error(
e: xee_xpath::error::Error,
src: Option<&str>,
) -> BunsenError {
let label = error_kind_label(&e.error);
let summary = e
.error
.message()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
let message = format!("XPath {label}: {}: {summary}", e.error.code());
let mut details = Vec::new();
if !e.error.note().is_empty() {
details.push(e.error.note().to_string());
}
if let Some(src) = src
&& let Some(src_span) = e.span
{
let rng = src_span.range();
let section = spanned_error_message::Section {
start: offset_to_location(src, rng.start),
end: offset_to_location(src, rng.end),
document: spanned_error_message::Document::from_content(src),
label: label.to_string(),
};
details.push(SpannedErrorMessage::new().create(§ion));
}
let error = match &e.error {
ErrorValue::XPST0003 => {
let mut parse = ParseError::new("XPath expression");
if let Some(src) = src {
parse = parse.input(src);
}
BunsenError::illegal(message).with_cause(parse.with_source(e))
}
ErrorValue::Unsupported => BunsenError::unsupported(message).with_cause(e),
ErrorValue::StackOverflow => BunsenError::sys(message).with_cause(e),
ErrorValue::UsedQueryWithWrongQueries => BunsenError::internal(message).with_cause(e),
_ => BunsenError::illegal(message).with_cause(e),
};
if details.is_empty() {
error
} else {
error.with_details(details.join("\n"))
}
}
#[cfg(test)]
mod tests {
use burn::nn::{
Linear,
LinearConfig,
};
use super::*;
use crate::{
burner::module::reflection::XmlModuleTree,
errors::{
BunsenErrorKind,
testing::{
ErrorMatcher,
predicate,
},
},
support::testing::cpu_device,
};
fn linear_tree() -> XmlModuleTree {
let module: Linear = LinearConfig::new(2, 3).init(&cpu_device());
XmlModuleTree::build(&module)
}
#[test]
fn test_evaluation_error_is_labelled_with_its_kind() {
let mut mtree = linear_tree();
let expr = "Linear/*[@name='weight', @rank=2]";
let err = mtree.select_param_ids(expr).unwrap_err();
ErrorMatcher::kind(BunsenErrorKind::Illegal)
.message_contains("XPath Type Error: XPTY0004")
.details_contains("Type Error")
.has_cause::<xee_xpath::error::Error>()
.assert(&err);
assert!(!format!("{err:#}").contains("Parse Error"), "{err:#}");
}
#[test]
fn test_syntax_error_is_labelled_parse_error() {
let mut mtree = linear_tree();
let err = mtree.try_select("Linear/*[").err().unwrap();
ErrorMatcher::kind(BunsenErrorKind::Illegal)
.message_contains("XPath Parse Error: XPST0003")
.details_contains("Parse Error")
.cause(predicate("the expression", |p: &ParseError| {
p.input
.as_deref()
.is_some_and(|i| i.ends_with("/Linear/*["))
}))
.assert(&err);
}
#[test]
fn test_error_kind_label() {
for (e, label) in [
(ErrorValue::XPST0003, "Parse Error"),
(ErrorValue::XPST0008, "Static Error"),
(ErrorValue::XPTY0004, "Type Error"),
(ErrorValue::XPDY0002, "Dynamic Error"),
(ErrorValue::FORG0006, "Function Error"),
(ErrorValue::StackOverflow, "XPath Error"),
] {
assert_eq!(error_kind_label(&e), label, "{e}");
}
}
#[test]
fn test_error() {
let e = ErrorValue::XPST0003;
println!("{}", pretty_errorvalue(&e));
println!("debug:{e:?}, display:{e}");
println!("msg: {}", e.message());
println!("note: {}", e.note());
}
}