Expose more verbose JS codec error output.

Closes #391.
This commit is contained in:
Orne Brocaar 2024-03-26 11:06:43 +00:00
parent f970e94cca
commit e3752fb19d

View File

@ -3,7 +3,7 @@ use std::time::SystemTime;
use anyhow::Result;
use chrono::{DateTime, Utc};
use rquickjs::IntoJs;
use rquickjs::{CatchResultExt, IntoJs};
use super::convert;
use crate::config;
@ -69,13 +69,16 @@ pub async fn decode(
globals.set("chirpstack_input", input)?;
globals.set("Buffer", buff)?;
let res: rquickjs::Object = ctx.eval_with_options(
let res: rquickjs::Object = ctx
.eval_with_options(
script,
rquickjs::context::EvalOptions {
strict: false,
..Default::default()
},
)?;
)
.catch(&ctx)
.map_err(|e| anyhow!("JS error: {}", e))?;
let errors: Result<Vec<String>, rquickjs::Error> = res.get("errors");
if let Ok(errors) = errors {
@ -152,13 +155,16 @@ pub async fn encode(
globals.set("chirpstack_input", input)?;
globals.set("Buffer", buff)?;
let res: rquickjs::Object = ctx.eval_with_options(
let res: rquickjs::Object = ctx
.eval_with_options(
script,
rquickjs::context::EvalOptions {
strict: false,
..Default::default()
},
)?;
)
.catch(&ctx)
.map_err(|e| anyhow!("JS error: {}", e))?;
let errors: Result<Vec<String>, rquickjs::Error> = res.get("errors");
if let Ok(errors) = errors {
@ -200,6 +206,24 @@ pub mod test {
assert!(out.is_err());
}
#[tokio::test]
pub async fn test_decode_error() {
let decoder = r#"
function decodeUplink(input) {
return foo;
}
"#
.to_string();
let vars: HashMap<String, String> = HashMap::new();
let out = decode(Utc::now(), 10, &vars, &decoder, &[0x01, 0x02, 0x03]).await;
assert_eq!(
"JS error: Error:4:24 'foo' is not defined\n at decodeUplink (eval_script:4:24)\n at <eval> (eval_script:8:9)\n",
out.err().unwrap().to_string()
);
}
#[tokio::test]
pub async fn test_decode() {
let recv_time = Utc.with_ymd_and_hms(2014, 7, 8, 9, 10, 11).unwrap();
@ -320,6 +344,25 @@ pub mod test {
assert!(out.is_err());
}
#[tokio::test]
pub async fn test_encode_error() {
let encoder = r#"
function encodeDownlink(input) {
return foo;
}
"#
.to_string();
let vars: HashMap<String, String> = HashMap::new();
let input = prost_types::Struct {
..Default::default()
};
let out = encode(10, &vars, &encoder, &input).await;
assert_eq!("JS error: Error:4:24 'foo' is not defined\n at encodeDownlink (eval_script:4:24)\n at <eval> (eval_script:8:9)\n", out.err().unwrap().to_string());
}
#[tokio::test]
pub async fn test_encode() {
let encoder = r#"