From d0212198b676066e2264666f6b728a246c16cc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 8 Nov 2022 03:16:24 +0100 Subject: [PATCH] refactor(ext/node): remove unwraps (#16559) --- ext/node/lib.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/ext/node/lib.rs b/ext/node/lib.rs index d9e5dcdbd5..ca3fe3c086 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 { } #[op] -fn op_require_path_dirname(state: &mut OpState, request: String) -> String { +fn op_require_path_dirname( + state: &mut OpState, + request: String, +) -> Result { 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 { 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]