1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

fix: fetch bad URL will not panic (#8884)

This commit is contained in:
Yosi Pramajaya 2020-12-26 20:06:00 +07:00 committed by GitHub
parent bfe1b05381
commit c1fdb30394
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 10 deletions

15
Cargo.lock generated
View file

@ -771,6 +771,16 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 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]] [[package]]
name = "from_variant" name = "from_variant"
version = "0.1.2" version = "0.1.2"
@ -3019,10 +3029,11 @@ checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]] [[package]]
name = "url" name = "url"
version = "2.1.1" version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" checksum = "5909f2b0817350449ed73e8bcd81c8c3c8d9a7a5d8acba4b27db277f1868976e"
dependencies = [ dependencies = [
"form_urlencoded",
"idna", "idna",
"matches", "matches",
"percent-encoding", "percent-encoding",

View file

@ -27,12 +27,37 @@ unitTest(
async (): Promise<void> => { async (): Promise<void> => {
await fetch("http://localhost:4000"); await fetch("http://localhost:4000");
}, },
Deno.errors.Http, TypeError,
"error trying to connect", "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< unitTest({ perms: { net: true } }, async function fetchJsonSuccess(): Promise<
void void
> { > {
@ -199,9 +224,12 @@ unitTest({ perms: { net: true } }, async function responseClone(): Promise<
unitTest({ perms: { net: true } }, async function fetchEmptyInvalid(): Promise< unitTest({ perms: { net: true } }, async function fetchEmptyInvalid(): Promise<
void void
> { > {
await assertThrowsAsync(async () => { await assertThrowsAsync(
async () => {
await fetch(""); await fetch("");
}, URIError); },
URIError,
);
}); });
unitTest( unitTest(

View file

@ -23,7 +23,7 @@ rusty_v8 = "0.14.0"
serde_json = { version = "1.0", features = ["preserve_order"] } serde_json = { version = "1.0", features = ["preserve_order"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
smallvec = "1.4.2" smallvec = "1.4.2"
url = { version = "2.1.1", features = ["serde"] } url = { version = "2.2", features = ["serde"] }
pin-project = "1.0.2" pin-project = "1.0.2"
[[example]] [[example]]

View file

@ -8,7 +8,6 @@ use deno_core::error::AnyError;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::serde_json::Value; use deno_core::serde_json::Value;
use deno_core::url;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::BufVec; use deno_core::BufVec;
@ -126,7 +125,7 @@ where
None => Method::GET, None => Method::GET,
}; };
let url_ = url::Url::parse(&url)?; let url_ = Url::parse(&url)?;
// Check scheme before asking for net permission // Check scheme before asking for net permission
let scheme = url_.scheme(); let scheme = url_.scheme();
@ -155,7 +154,10 @@ where
} }
//debug!("Before fetch {}", url); //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); //debug!("Fetch response {}", url);
let status = res.status(); let status = res.status();