Skip to content

Commit

Permalink
refactor: use concrete error types and JsError (#107)
Browse files Browse the repository at this point in the history
Required for denoland/deno#26867
  • Loading branch information
crowlKats authored Jan 8, 2025
1 parent a9eb782 commit bc43587
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 134 deletions.
72 changes: 59 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ rusqlite = "0.32.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0.107"
tempfile = "3"
thiserror = "1"
thiserror = "2"
deno_error = { version = "0.5.3", features = ["url", "serde_json", "serde"] }
tokio = { version = "1.33.0", features = ["full"] }
tokio-stream = "0.1"
tokio-util = { version = "0.7", features = ["full"] }
Expand Down
1 change: 1 addition & 0 deletions denokv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ serde.workspace = true
thiserror.workspace = true
tokio.workspace = true
uuid.workspace = true
deno_error.workspace = true

[dev-dependencies]
bytes.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions denokv/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ fn open_sqlite(
let sqlite = Sqlite::new(
|| {
Ok((
Connection::open_with_flags(path, flags)?,
Connection::open_with_flags(path, flags)
.map_err(|e| deno_error::JsErrorBox::generic(e.to_string()))?,
Box::new(rand::rngs::StdRng::from_entropy()),
))
},
Expand Down Expand Up @@ -493,7 +494,7 @@ async fn watch_endpoint(
timer.set_missed_tick_behavior(MissedTickBehavior::Delay);
let ping_stream = unfold(timer, |mut timer| async move {
timer.tick().await;
Some((Ok::<_, anyhow::Error>(vec![]), timer))
Some((Ok::<_, deno_error::JsErrorBox>(vec![]), timer))
})
.boxed();

Expand Down
38 changes: 28 additions & 10 deletions denokv/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::process::Stdio;
use std::time::Duration;

use bytes::Bytes;
use deno_error::JsErrorBox;
use denokv_proto::AtomicWrite;
use denokv_proto::Database;
use denokv_proto::KvEntry;
use denokv_proto::KvValue;
use denokv_proto::ReadRange;
use denokv_proto::WatchKeyOutput;
Expand Down Expand Up @@ -40,25 +40,43 @@ impl RemoteTransport for ReqwestClient {
url: Url,
headers: http::HeaderMap,
body: Bytes,
) -> Result<(Url, http::StatusCode, Self::Response), anyhow::Error> {
let res = self.0.post(url).headers(headers).body(body).send().await?;
) -> Result<(Url, http::StatusCode, Self::Response), JsErrorBox> {
let res = self
.0
.post(url)
.headers(headers)
.body(body)
.send()
.await
.map_err(|e| JsErrorBox::generic(e.to_string()))?;
let url = res.url().clone();
let status = res.status();
Ok((url, status, ReqwestResponse(res)))
}
}

impl RemoteResponse for ReqwestResponse {
async fn bytes(self) -> Result<Bytes, anyhow::Error> {
Ok(self.0.bytes().await?)
async fn bytes(self) -> Result<Bytes, JsErrorBox> {
self
.0
.bytes()
.await
.map_err(|e| JsErrorBox::generic(e.to_string()))
}
fn stream(
self,
) -> impl Stream<Item = Result<Bytes, anyhow::Error>> + Send + Sync {
self.0.bytes_stream().map_err(|e| e.into())
) -> impl Stream<Item = Result<Bytes, JsErrorBox>> + Send + Sync {
self
.0
.bytes_stream()
.map_err(|e| JsErrorBox::generic(e.to_string()))
}
async fn text(self) -> Result<String, anyhow::Error> {
Ok(self.0.text().await?)
async fn text(self) -> Result<String, JsErrorBox> {
self
.0
.text()
.await
.map_err(|e| JsErrorBox::generic(e.to_string()))
}
}

Expand Down Expand Up @@ -136,7 +154,7 @@ async fn start_server() -> (tokio::process::Child, SocketAddr) {
struct DummyPermissions;

impl denokv_remote::RemotePermissions for DummyPermissions {
fn check_net_url(&self, _url: &reqwest::Url) -> Result<(), anyhow::Error> {
fn check_net_url(&self, _url: &reqwest::Url) -> Result<(), JsErrorBox> {
Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions npm/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ anyhow = "1"
prost = "0.13"
once_cell = "1.18.0"
rusqlite = { version = "0.32.0", features = ["bundled"] }
deno_error = "0.5.3"

[build-dependencies]
napi-build = "2"
Expand Down
10 changes: 7 additions & 3 deletions npm/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn open(path: String, in_memory: Option<bool>, debug: bool) -> Result<u32> {
|| {
Ok((
Connection::open_with_flags(Path::new(&path), flags)
.map_err(anyhow::Error::from)?,
.map_err(|e| deno_error::JsErrorBox::generic(e.to_string()))?,
Box::new(rand::rngs::StdRng::from_entropy()),
))
},
Expand All @@ -67,7 +67,8 @@ pub fn open(path: String, in_memory: Option<bool>, debug: bool) -> Result<u32> {
num_workers: 1,
batch_timeout: None,
},
)?;
)
.map_err(anyhow::Error::from)?;

let db_id = DB_ID.fetch_add(1, Ordering::Relaxed);
DBS.lock().unwrap().insert(db_id, sqlite);
Expand Down Expand Up @@ -285,7 +286,10 @@ pub async fn start_watch(
.get(&db_id)
.cloned()
.ok_or_else(|| anyhow::anyhow!("db not found"))?;
let stream = db.watch(keys).map_err(|e| e.into()).boxed();
let stream = db
.watch(keys)
.map_err(|e| anyhow::Error::from(e).into())
.boxed();

let watch_id = WATCH_ID.fetch_add(1, Ordering::Relaxed);
WATCHES.lock().unwrap().insert(watch_id, stream);
Expand Down
2 changes: 1 addition & 1 deletion proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ path = "lib.rs"
build_protos = ["prost-build"]

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
chrono.workspace = true
futures.workspace = true
num-bigint.workspace = true
prost.workspace = true
serde.workspace = true
uuid.workspace = true
deno_error.workspace = true

[build-dependencies]
prost-build = { workspace = true, optional = true }
Loading

0 comments on commit bc43587

Please sign in to comment.