use crate::validation_error::ValidationError;
use wasm_bindgen::prelude::*;
pub fn to_js_error(err: ValidationError) -> JsValue {
let code = err.error_code().as_str();
let error_type = match &err {
ValidationError::SyntaxError { .. } => "SyntaxError",
ValidationError::TypeError { .. } => "TypeError",
ValidationError::UnitError { .. } => "UnitError",
ValidationError::UndefinedReference { .. } => "ReferenceError",
ValidationError::ScopeError { .. } => "ScopeError",
ValidationError::DuplicateDeclaration { .. } => "DuplicateDeclaration",
ValidationError::DeterminismError { .. } => "DeterminismError",
ValidationError::InvalidExpression { .. } => "InvalidExpression",
};
let message = err.to_string();
let metadata = match err {
ValidationError::SyntaxError {
line,
column,
end_line,
end_column,
..
} => {
serde_json::json!({
"code": code,
"errorType": error_type,
"line": line,
"column": column,
"endLine": end_line,
"endColumn": end_column,
})
}
ValidationError::TypeError {
expected_type,
found_type,
suggestion,
..
} => {
serde_json::json!({
"code": code,
"errorType": error_type,
"expected": expected_type,
"found": found_type,
"suggestion": suggestion,
})
}
ValidationError::UnitError {
expected,
found,
suggestion,
..
} => {
serde_json::json!({
"code": code,
"errorType": error_type,
"expectedDimension": format!("{}", expected),
"foundDimension": format!("{}", found),
"suggestion": suggestion,
})
}
ValidationError::UndefinedReference {
reference_type,
name,
suggestion,
..
} => {
serde_json::json!({
"code": code,
"errorType": error_type,
"referenceType": reference_type,
"name": name,
"suggestion": suggestion,
})
}
ValidationError::ScopeError {
variable,
available_in,
suggestion,
..
} => {
serde_json::json!({
"code": code,
"errorType": error_type,
"variable": variable,
"availableIn": available_in,
"suggestion": suggestion,
})
}
ValidationError::DuplicateDeclaration {
name,
first_location,
second_location,
} => {
serde_json::json!({
"code": code,
"errorType": error_type,
"name": name,
"firstLocation": first_location,
"secondLocation": second_location,
})
}
ValidationError::DeterminismError { hint, .. } => {
serde_json::json!({
"code": code,
"errorType": error_type,
"hint": hint,
})
}
ValidationError::InvalidExpression { suggestion, .. } => {
serde_json::json!({
"code": code,
"errorType": error_type,
"suggestion": suggestion,
})
}
};
let error_object = serde_json::json!({
"message": message,
"metadata": metadata
});
match serde_wasm_bindgen::to_value(&error_object) {
Ok(val) => val,
Err(_) => {
JsValue::from_str(&message)
}
}
}