From addfdc4cd0ffa0e0f6b284379c8873a202af7d5b Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Tue, 24 Mar 2020 20:50:51 -0700 Subject: [PATCH] fix: add fsEvent notify::Error casts (#4488) --- cli/js/tests/fs_events_test.ts | 20 ++++++++++++++++++++ cli/op_error.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/cli/js/tests/fs_events_test.ts b/cli/js/tests/fs_events_test.ts index b1697971ad..8494bf6af5 100644 --- a/cli/js/tests/fs_events_test.ts +++ b/cli/js/tests/fs_events_test.ts @@ -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 ): Promise { diff --git a/cli/op_error.rs b/cli/op_error.rs index bebfd2e723..3537d4b287 100644 --- a/cli/op_error.rs +++ b/cli/op_error.rs @@ -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; @@ -349,6 +350,30 @@ impl From<&dlopen::Error> for OpError { } } +impl From for OpError { + fn from(error: notify::Error) -> Self { + OpError::from(&error) + } +} + +impl From<¬ify::Error> for OpError { + fn from(error: ¬ify::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 for OpError { fn from(error: ErrBox) -> Self { #[cfg(unix)] @@ -384,6 +409,7 @@ impl From for OpError { .map(|e| e.into()) }) .or_else(|| error.downcast_ref::().map(|e| e.into())) + .or_else(|| error.downcast_ref::().map(|e| e.into())) .or_else(|| unix_error_kind(&error)) .unwrap_or_else(|| { panic!("Can't downcast {:?} to OpError", error);