Skip to content

Commit

Permalink
fix: add fsEvent notify::Error casts (denoland#4488)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Mar 25, 2020
1 parent 07fc95a commit addfdc4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cli/js/tests/fs_events_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ unitTest({ perms: { read: false } }, function fsEventsPermissions() {
assert(thrown);
});

unitTest({ perms: { read: true } }, function fsEventsInvalidPath() {
let thrown = false;
try {
Deno.fsEvents("non-existant.file");
} catch (err) {
console.error(err);
if (Deno.build.os === "win") {
assert(
err.message.includes(
"Input watch path is neither a file nor a directory"
)
);
} else {
assert(err instanceof Deno.errors.NotFound);
}
thrown = true;
}
assert(thrown);
});

async function getTwoEvents(
iter: AsyncIterableIterator<Deno.FsEvent>
): Promise<Deno.FsEvent[]> {
Expand Down
26 changes: 26 additions & 0 deletions cli/op_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::import_map::ImportMapError;
use deno_core::ErrBox;
use deno_core::ModuleResolutionError;
use dlopen;
use notify;
use reqwest;
use rustyline::error::ReadlineError;
use std;
Expand Down Expand Up @@ -349,6 +350,30 @@ impl From<&dlopen::Error> for OpError {
}
}

impl From<notify::Error> for OpError {
fn from(error: notify::Error) -> Self {
OpError::from(&error)
}
}

impl From<&notify::Error> for OpError {
fn from(error: &notify::Error) -> Self {
use notify::ErrorKind::*;
let kind = match error.kind {
Generic(_) => ErrorKind::Other,
Io(ref e) => return e.into(),
PathNotFound => ErrorKind::NotFound,
WatchNotFound => ErrorKind::NotFound,
InvalidConfig(_) => ErrorKind::InvalidData,
};

Self {
kind,
msg: error.to_string(),
}
}
}

impl From<ErrBox> for OpError {
fn from(error: ErrBox) -> Self {
#[cfg(unix)]
Expand Down Expand Up @@ -384,6 +409,7 @@ impl From<ErrBox> for OpError {
.map(|e| e.into())
})
.or_else(|| error.downcast_ref::<dlopen::Error>().map(|e| e.into()))
.or_else(|| error.downcast_ref::<notify::Error>().map(|e| e.into()))
.or_else(|| unix_error_kind(&error))
.unwrap_or_else(|| {
panic!("Can't downcast {:?} to OpError", error);
Expand Down

0 comments on commit addfdc4

Please sign in to comment.