Store the NetworkError in network error Responses.

This commit is contained in:
Ms2ger 2016-10-19 15:11:12 +02:00
parent 6af63d6af1
commit 784c45a306
3 changed files with 69 additions and 55 deletions

View file

@ -14,12 +14,12 @@ use std::sync::{Arc, Mutex};
use url::Url;
/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
#[derive(Clone, PartialEq, Copy, Debug, Deserialize, Serialize, HeapSizeOf)]
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize, HeapSizeOf)]
pub enum ResponseType {
Basic,
CORS,
Default,
Error,
Error(NetworkError),
Opaque,
OpaqueRedirect
}
@ -116,9 +116,9 @@ impl Response {
}
}
pub fn network_error() -> Response {
pub fn network_error(e: NetworkError) -> Response {
Response {
response_type: ResponseType::Error,
response_type: ResponseType::Error(e),
termination_reason: None,
url: None,
url_list: RefCell::new(vec![]),
@ -135,11 +135,18 @@ impl Response {
pub fn is_network_error(&self) -> bool {
match self.response_type {
ResponseType::Error => true,
ResponseType::Error(..) => true,
_ => false
}
}
pub fn get_network_error(&self) -> Option<&NetworkError> {
match self.response_type {
ResponseType::Error(ref e) => Some(e),
_ => None,
}
}
pub fn actual_response(&self) -> &Response {
if self.return_internal.get() && self.internal_response.is_some() {
&**self.internal_response.as_ref().unwrap()
@ -159,13 +166,15 @@ impl Response {
/// Convert to a filtered response, of type `filter_type`.
/// Do not use with type Error or Default
pub fn to_filtered(self, filter_type: ResponseType) -> Response {
assert!(filter_type != ResponseType::Error);
assert!(filter_type != ResponseType::Default);
match filter_type {
ResponseType::Default | ResponseType::Error(..) => panic!(),
_ => (),
}
let old_response = self.to_actual();
if Response::is_network_error(&old_response) {
return Response::network_error();
if let ResponseType::Error(e) = old_response.response_type {
return Response::network_error(e);
}
let old_headers = old_response.headers.clone();
@ -173,8 +182,8 @@ impl Response {
response.internal_response = Some(Box::new(old_response));
response.response_type = filter_type;
match filter_type {
ResponseType::Default | ResponseType::Error => unreachable!(),
match response.response_type {
ResponseType::Default | ResponseType::Error(..) => unreachable!(),
ResponseType::Basic => {
let headers = old_headers.iter().filter(|header| {
@ -238,8 +247,8 @@ impl Response {
metadata
};
if self.is_network_error() {
return Err(NetworkError::Internal("Cannot extract metadata from network error".to_owned()));
if let Some(error) = self.get_network_error() {
return Err(error.clone());
}
let metadata = self.url.as_ref().map(|url| init_metadata(self, url));