Skip to content

Commit

Permalink
fix: handle status in error
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Apr 26, 2022
1 parent 16e215a commit e29c0ed
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ napi = { version = "2.1.0", default-features = false, features = [
"async"
] }
napi-derive = "2.1.0"
async-compression = { version = "0.3.12", features = [
"tokio",
"zstd"
] }
tokio = { version = "1", features = [
"full"
] }
zstd = "0.11"

[build-dependencies]
napi-build = "1.2.1"
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ extern crate napi_derive;

use napi::{
bindgen_prelude::AsyncTask,
Env, JsBuffer, JsBufferValue, Ref, Result, Task,
Env, Error, JsBuffer, JsBufferValue, Ref, Result, Status, Task,
};
use zstd::stream::{encode_all, decode_all};

struct Encoder {
data: Ref<JsBufferValue>
Expand All @@ -17,7 +18,7 @@ impl Task for Encoder {

fn compute(&mut self) -> Result<Self::Output> {
let data: &[u8] = self.data.as_ref();
Ok(data.to_vec())
encode_all(data, 3).map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
}

fn resolve(&mut self, env: Env, output: Self::Output) -> Result<JsBuffer> {
Expand All @@ -41,7 +42,7 @@ impl Task for Decoder {

fn compute(&mut self) -> Result<Self::Output> {
let data: &[u8] = self.data.as_ref();
Ok(data.to_vec())
decode_all(data).map_err(|e| Error::new(Status::GenericFailure, format!("{}", e)))
}

fn resolve(&mut self, env: Env, output: Self::Output) -> Result<JsBuffer> {
Expand Down

0 comments on commit e29c0ed

Please sign in to comment.