From 945dc7b84bc1a05f80b3b4e0b152be38d9f8a9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 11 Sep 2019 23:34:22 +0200 Subject: [PATCH] fix: panic during fetch (#2925) --- cli/deno_error.rs | 22 ++++++++++++++++++++++ core/any_error.rs | 2 +- js/fetch_test.ts | 13 +++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cli/deno_error.rs b/cli/deno_error.rs index 36d8b724ae..71e5ac806e 100644 --- a/cli/deno_error.rs +++ b/cli/deno_error.rs @@ -8,6 +8,7 @@ use deno::ErrBox; use deno::ModuleResolutionError; use http::uri; use hyper; +use reqwest; use rustyline::error::ReadlineError; use std; 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::().map(Get::kind)) + .or_else(|| err_ref.downcast_ref::().map(Get::kind)) + .or_else(|| err_ref.downcast_ref::().map(Get::kind)) + .or_else(|| { + err_ref + .downcast_ref::() + .map(Get::kind) + }) + .unwrap_or_else(|| ErrorKind::HttpOther), + _ => ErrorKind::HttpOther, + } + } +} + impl GetErrorKind for ReadlineError { fn kind(&self) -> ErrorKind { use ReadlineError::*; @@ -254,6 +275,7 @@ impl GetErrorKind for dyn AnyError { .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) + .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) .or_else(|| self.downcast_ref::().map(Get::kind)) diff --git a/core/any_error.rs b/core/any_error.rs index 9199af59e0..60c508e7d1 100644 --- a/core/any_error.rs +++ b/core/any_error.rs @@ -5,7 +5,7 @@ use std::fmt; use std::ops::Deref; // 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 {} impl AnyError for T where T: Any + Error + Send + Sync + Sized + 'static {} diff --git a/js/fetch_test.ts b/js/fetch_test.ts index 77cc010a8d..4fcc39f010 100644 --- a/js/fetch_test.ts +++ b/js/fetch_test.ts @@ -4,9 +4,22 @@ import { testPerm, assert, assertEquals, + assertStrContains, assertThrows } from "./test_util.ts"; +testPerm({ net: true }, async function fetchConnectionError(): Promise { + 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 { const response = await fetch("http://localhost:4545/package.json"); const json = await response.json();