summaryrefslogtreecommitdiff
path: root/ext/node/lib.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-11-08 03:16:24 +0100
committerGitHub <noreply@github.com>2022-11-08 03:16:24 +0100
commitd0212198b676066e2264666f6b728a246c16cc0d (patch)
tree244ea8376ae9f538b3791842930a09e7613cec79 /ext/node/lib.rs
parent3019c45f87c1825d10ab033e1c51832cafefd5b1 (diff)
refactor(ext/node): remove unwraps (#16559)
Diffstat (limited to 'ext/node/lib.rs')
-rw-r--r--ext/node/lib.rs33
1 files changed, 26 insertions, 7 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs
index d9e5dcdbd..ca3fe3c08 100644
--- a/ext/node/lib.rs
+++ b/ext/node/lib.rs
@@ -1,5 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::include_js_files;
use deno_core::normalize_path;
@@ -431,17 +432,31 @@ fn op_require_path_resolve(state: &mut OpState, parts: Vec<String>) -> String {
}
#[op]
-fn op_require_path_dirname(state: &mut OpState, request: String) -> String {
+fn op_require_path_dirname(
+ state: &mut OpState,
+ request: String,
+) -> Result<String, AnyError> {
check_unstable(state);
let p = PathBuf::from(request);
- p.parent().unwrap().to_string_lossy().to_string()
+ if let Some(parent) = p.parent() {
+ Ok(parent.to_string_lossy().to_string())
+ } else {
+ Err(generic_error("Path doesn't have a parent"))
+ }
}
#[op]
-fn op_require_path_basename(state: &mut OpState, request: String) -> String {
+fn op_require_path_basename(
+ state: &mut OpState,
+ request: String,
+) -> Result<String, AnyError> {
check_unstable(state);
let p = PathBuf::from(request);
- p.file_name().unwrap().to_string_lossy().to_string()
+ if let Some(path) = p.file_name() {
+ Ok(path.to_string_lossy().to_string())
+ } else {
+ Err(generic_error("Path doesn't have a file name"))
+ }
}
#[op]
@@ -551,10 +566,14 @@ pub fn op_require_as_file_path(
file_or_url: String,
) -> String {
check_unstable(state);
- match Url::parse(&file_or_url) {
- Ok(url) => url.to_file_path().unwrap().to_string_lossy().to_string(),
- Err(_) => file_or_url,
+
+ if let Ok(url) = Url::parse(&file_or_url) {
+ if let Ok(p) = url.to_file_path() {
+ return p.to_string_lossy().to_string();
+ }
}
+
+ file_or_url
}
#[op]