diff options
-rw-r--r-- | Cargo.lock | 15 | ||||
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 36 | ||||
-rw-r--r-- | core/Cargo.toml | 2 | ||||
-rw-r--r-- | op_crates/fetch/lib.rs | 8 |
4 files changed, 51 insertions, 10 deletions
diff --git a/Cargo.lock b/Cargo.lock index 0f8f99773..f74fe8436 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -772,6 +772,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] +name = "form_urlencoded" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece68d15c92e84fa4f19d3780f1294e5ca82a78a6d515f1efaabcc144688be00" +dependencies = [ + "matches", + "percent-encoding", +] + +[[package]] name = "from_variant" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3019,10 +3029,11 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" [[package]] name = "url" -version = "2.1.1" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +checksum = "5909f2b0817350449ed73e8bcd81c8c3c8d9a7a5d8acba4b27db277f1868976e" dependencies = [ + "form_urlencoded", "idna", "matches", "percent-encoding", diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index 359a24e95..2355d0813 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -27,12 +27,37 @@ unitTest( async (): Promise<void> => { await fetch("http://localhost:4000"); }, - Deno.errors.Http, + TypeError, "error trying to connect", ); }, ); +unitTest( + { perms: { net: true } }, + async function fetchDnsError(): Promise<void> { + await assertThrowsAsync( + async (): Promise<void> => { + await fetch("http://nil/"); + }, + TypeError, + "error trying to connect", + ); + }, +); + +unitTest( + { perms: { net: true } }, + async function fetchInvalidUriError(): Promise<void> { + await assertThrowsAsync( + async (): Promise<void> => { + await fetch("http://<invalid>/"); + }, + URIError, + ); + }, +); + unitTest({ perms: { net: true } }, async function fetchJsonSuccess(): Promise< void > { @@ -199,9 +224,12 @@ unitTest({ perms: { net: true } }, async function responseClone(): Promise< unitTest({ perms: { net: true } }, async function fetchEmptyInvalid(): Promise< void > { - await assertThrowsAsync(async () => { - await fetch(""); - }, URIError); + await assertThrowsAsync( + async () => { + await fetch(""); + }, + URIError, + ); }); unitTest( diff --git a/core/Cargo.toml b/core/Cargo.toml index 4e81f935c..2d4abc2cf 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -23,7 +23,7 @@ rusty_v8 = "0.14.0" serde_json = { version = "1.0", features = ["preserve_order"] } serde = { version = "1.0", features = ["derive"] } smallvec = "1.4.2" -url = { version = "2.1.1", features = ["serde"] } +url = { version = "2.2", features = ["serde"] } pin-project = "1.0.2" [[example]] diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index 4bc37b998..91e44f75c 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -8,7 +8,6 @@ use deno_core::error::AnyError; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; -use deno_core::url; use deno_core::url::Url; use deno_core::AsyncRefCell; use deno_core::BufVec; @@ -126,7 +125,7 @@ where None => Method::GET, }; - let url_ = url::Url::parse(&url)?; + let url_ = Url::parse(&url)?; // Check scheme before asking for net permission let scheme = url_.scheme(); @@ -155,7 +154,10 @@ where } //debug!("Before fetch {}", url); - let res = request.send().await?; + let res = match request.send().await { + Ok(res) => res, + Err(e) => return Err(type_error(e.to_string())), + }; //debug!("Fetch response {}", url); let status = res.status(); |