fix rewriter bin

This commit is contained in:
Toshit Chawda 2024-10-21 16:23:39 -07:00
parent 5d809faa75
commit a3bed65986
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
4 changed files with 63 additions and 55 deletions

45
rewriter/src/error.rs Normal file
View file

@ -0,0 +1,45 @@
use thiserror::Error;
use wasm_bindgen::{JsError, JsValue};
#[derive(Debug, Error)]
pub enum RewriterError {
#[error("JS: {0}")]
Js(String),
#[error("URL parse error: {0}")]
Url(#[from] url::ParseError),
#[error("str fromutf8 error: {0}")]
Str(#[from] std::str::Utf8Error),
#[error("{0} was not {1}")]
Not(String, &'static str),
#[error("❗❗❗ ❗❗❗ REWRITER OFFSET OOB FAIL ❗❗❗ ❗❗❗")]
Oob,
}
impl From<JsValue> for RewriterError {
fn from(value: JsValue) -> Self {
Self::Js(format!("{:?}", value))
}
}
impl From<RewriterError> for JsValue {
fn from(value: RewriterError) -> Self {
JsError::from(value).into()
}
}
impl RewriterError {
pub fn not_str(x: &str, obj: &JsValue) -> Self {
Self::Not(format!("{:?} in {:?}", x, obj), "string")
}
pub fn not_fn(obj: JsValue) -> Self {
Self::Not(format!("{:?}", obj), "function")
}
pub fn not_bool(obj: &JsValue) -> Self {
Self::Not(format!("{:?}", obj), "bool")
}
}
pub type Result<T> = std::result::Result<T, RewriterError>;