mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 20:25:12 -05:00
fix: panic during fetch (#2925)
This commit is contained in:
parent
82dc1b8e59
commit
945dc7b84b
3 changed files with 36 additions and 1 deletions
|
@ -8,6 +8,7 @@ use deno::ErrBox;
|
||||||
use deno::ModuleResolutionError;
|
use deno::ModuleResolutionError;
|
||||||
use http::uri;
|
use http::uri;
|
||||||
use hyper;
|
use hyper;
|
||||||
|
use reqwest;
|
||||||
use rustyline::error::ReadlineError;
|
use rustyline::error::ReadlineError;
|
||||||
use std;
|
use std;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
@ -190,6 +191,26 @@ impl GetErrorKind for hyper::Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GetErrorKind for reqwest::Error {
|
||||||
|
fn kind(&self) -> ErrorKind {
|
||||||
|
use self::GetErrorKind as Get;
|
||||||
|
|
||||||
|
match self.get_ref() {
|
||||||
|
Some(err_ref) => None
|
||||||
|
.or_else(|| err_ref.downcast_ref::<hyper::Error>().map(Get::kind))
|
||||||
|
.or_else(|| err_ref.downcast_ref::<url::ParseError>().map(Get::kind))
|
||||||
|
.or_else(|| err_ref.downcast_ref::<io::Error>().map(Get::kind))
|
||||||
|
.or_else(|| {
|
||||||
|
err_ref
|
||||||
|
.downcast_ref::<serde_json::error::Error>()
|
||||||
|
.map(Get::kind)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| ErrorKind::HttpOther),
|
||||||
|
_ => ErrorKind::HttpOther,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl GetErrorKind for ReadlineError {
|
impl GetErrorKind for ReadlineError {
|
||||||
fn kind(&self) -> ErrorKind {
|
fn kind(&self) -> ErrorKind {
|
||||||
use ReadlineError::*;
|
use ReadlineError::*;
|
||||||
|
@ -254,6 +275,7 @@ impl GetErrorKind for dyn AnyError {
|
||||||
.or_else(|| self.downcast_ref::<DenoError>().map(Get::kind))
|
.or_else(|| self.downcast_ref::<DenoError>().map(Get::kind))
|
||||||
.or_else(|| self.downcast_ref::<Diagnostic>().map(Get::kind))
|
.or_else(|| self.downcast_ref::<Diagnostic>().map(Get::kind))
|
||||||
.or_else(|| self.downcast_ref::<hyper::Error>().map(Get::kind))
|
.or_else(|| self.downcast_ref::<hyper::Error>().map(Get::kind))
|
||||||
|
.or_else(|| self.downcast_ref::<reqwest::Error>().map(Get::kind))
|
||||||
.or_else(|| self.downcast_ref::<ImportMapError>().map(Get::kind))
|
.or_else(|| self.downcast_ref::<ImportMapError>().map(Get::kind))
|
||||||
.or_else(|| self.downcast_ref::<io::Error>().map(Get::kind))
|
.or_else(|| self.downcast_ref::<io::Error>().map(Get::kind))
|
||||||
.or_else(|| self.downcast_ref::<JSError>().map(Get::kind))
|
.or_else(|| self.downcast_ref::<JSError>().map(Get::kind))
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::fmt;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
// The Send and Sync traits are required because deno is multithreaded and we
|
// The Send and Sync traits are required because deno is multithreaded and we
|
||||||
// need to beable to handle errors across threads.
|
// need to be able to handle errors across threads.
|
||||||
pub trait AnyError: Any + Error + Send + Sync + 'static {}
|
pub trait AnyError: Any + Error + Send + Sync + 'static {}
|
||||||
impl<T> AnyError for T where T: Any + Error + Send + Sync + Sized + 'static {}
|
impl<T> AnyError for T where T: Any + Error + Send + Sync + Sized + 'static {}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,22 @@ import {
|
||||||
testPerm,
|
testPerm,
|
||||||
assert,
|
assert,
|
||||||
assertEquals,
|
assertEquals,
|
||||||
|
assertStrContains,
|
||||||
assertThrows
|
assertThrows
|
||||||
} from "./test_util.ts";
|
} from "./test_util.ts";
|
||||||
|
|
||||||
|
testPerm({ net: true }, async function fetchConnectionError(): Promise<void> {
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
await fetch("http://localhost:4000");
|
||||||
|
} catch (err_) {
|
||||||
|
err = err_;
|
||||||
|
}
|
||||||
|
assertEquals(err.kind, Deno.ErrorKind.HttpOther);
|
||||||
|
assertEquals(err.name, "HttpOther");
|
||||||
|
assertStrContains(err.message, "error trying to connect");
|
||||||
|
});
|
||||||
|
|
||||||
testPerm({ net: true }, async function fetchJsonSuccess(): Promise<void> {
|
testPerm({ net: true }, async function fetchJsonSuccess(): Promise<void> {
|
||||||
const response = await fetch("http://localhost:4545/package.json");
|
const response = await fetch("http://localhost:4545/package.json");
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
|
|
Loading…
Add table
Reference in a new issue