Commit 60ebed06 authored by horo@chromium.org's avatar horo@chromium.org

[ServiceWorker] Provide more descriptive error messages when fetch API fails.

BUG=429060
TEST=http/tests/serviceworker/

Review URL: https://codereview.chromium.org/728713003

git-svn-id: svn://svn.chromium.org/blink/trunk@185418 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent e1cf9db2
<!DOCTYPE html>
<title>Service Worker: Error messages for fetch()</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>
<script>
service_worker_test(
'resources/fetch-error-messages-worker.js',
'Error messages for fetch()');
</script>
importScripts('../../resources/worker-testharness.js');
importScripts('../../resources/test-helpers.js');
async_test(function(t) {
var url = get_host_info()['HTTP_REMOTE_ORIGIN'] + '/dummy.html';
fetch(new Request(url, {mode: 'same-origin'}))
.then(
t.unreached_func('Fetching must fail.'),
function(e) {
assert_equals(
e.message,
'Fetch API cannot load ' + url + '. ' +
'Request mode is "same-origin" but the URL\'s origin is not same ' +
'as the request origin ' + get_host_info()['HTTP_ORIGIN'] + '.');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch API error message - not same origin request');
async_test(function(t) {
var url = 'ftp://example.com/dummy.html';
fetch(new Request(url, {mode: 'cors'}))
.then(
t.unreached_func('Fetching must fail.'),
function(e) {
assert_equals(
e.message,
'Fetch API cannot load ' + url + '. ' +
'URL scheme must be "http" or "https" for CORS request.');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch API error message - non http cors request');
async_test(function(t) {
var url = 'about://blank';
fetch(new Request(url))
.then(
t.unreached_func('Fetching must fail.'),
function(e) {
assert_equals(
e.message,
'Fetch API cannot load ' + url + '. ' +
'URL scheme "about" is not supported.');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch API error message - unsupported scheme.');
async_test(function(t) {
var url =
new URL(get_host_info()['HTTP_ORIGIN'] + base_path() +
'../../resources/invalid-chunked-encoding.php').toString();
fetch(new Request(url))
.then(
t.unreached_func('Fetching must fail.'),
function(e) {
assert_equals(
e.message,
'Fetch API cannot load ' + url + '. ' +
'net::ERR_INVALID_CHUNKED_ENCODING');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch API error message - invalid chunked encoding.');
async_test(function(t) {
var url =
new URL(get_host_info()['HTTP_REMOTE_ORIGIN'] + base_path() +
'../../resources/fetch-access-control.php').toString();
fetch(new Request(url))
.then(
t.unreached_func('Fetching must fail.'),
function(e) {
assert_equals(
e.message,
'Fetch API cannot load ' + url + '. ' +
'No \'Access-Control-Allow-Origin\' header is present on the ' +
'requested resource. Origin \'' + get_host_info()['HTTP_ORIGIN'] +
'\' is therefore not allowed access.');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch API error message - cors error.');
async_test(function(t) {
// FIXME: When we support the redirection in Fech API, we have to change
// this test case.
var redirect = 'http://www.example.com';
var url =
new URL(get_host_info()['HTTP_REMOTE_ORIGIN'] + base_path() +
'../../resources/redirect.php?Redirect=' + redirect).toString();
fetch(new Request(url))
.then(
t.unreached_func('Fetching must fail.'),
function(e) {
assert_equals(
e.message,
'Fetch API cannot load ' + url + '. ' +
'Redirects are not yet supported.');
t.done();
})
.catch(unreached_rejection(t));
}, 'Fetch API error message - redirect error.');
...@@ -40,9 +40,9 @@ public: ...@@ -40,9 +40,9 @@ public:
private: private:
void performBasicFetch(); void performBasicFetch();
void performNetworkError(); void performNetworkError(const String& message);
void performHTTPFetch(); void performHTTPFetch();
void failed(); void failed(const String& message);
void notifyFinished(); void notifyFinished();
ExecutionContext* m_executionContext; ExecutionContext* m_executionContext;
...@@ -120,17 +120,17 @@ void FetchManager::Loader::didFinishLoading(unsigned long, double) ...@@ -120,17 +120,17 @@ void FetchManager::Loader::didFinishLoading(unsigned long, double)
void FetchManager::Loader::didFail(const ResourceError& error) void FetchManager::Loader::didFail(const ResourceError& error)
{ {
failed(); failed("Fetch API cannot load " + error.failingURL() + ". " + error.localizedDescription());
} }
void FetchManager::Loader::didFailAccessControlCheck(const ResourceError& error) void FetchManager::Loader::didFailAccessControlCheck(const ResourceError& error)
{ {
failed(); failed("Fetch API cannot load " + error.failingURL() + ". " + error.localizedDescription());
} }
void FetchManager::Loader::didFailRedirectCheck() void FetchManager::Loader::didFailRedirectCheck()
{ {
failed(); failed("Fetch API cannot load " + m_request->url().string() + ". Redirects are not yet supported.");
} }
void FetchManager::Loader::didDownloadData(int dataLength) void FetchManager::Loader::didDownloadData(int dataLength)
...@@ -179,7 +179,7 @@ void FetchManager::Loader::start() ...@@ -179,7 +179,7 @@ void FetchManager::Loader::start()
// "- |request|'s mode is |same-origin|" // "- |request|'s mode is |same-origin|"
if (m_request->mode() == WebURLRequest::FetchRequestModeSameOrigin) { if (m_request->mode() == WebURLRequest::FetchRequestModeSameOrigin) {
// "A network error." // "A network error."
performNetworkError(); performNetworkError("Fetch API cannot load " + m_request->url().string() + ". Request mode is \"same-origin\" but the URL\'s origin is not same as the request origin " + m_request->origin()->toString() + ".");
return; return;
} }
...@@ -195,7 +195,7 @@ void FetchManager::Loader::start() ...@@ -195,7 +195,7 @@ void FetchManager::Loader::start()
// "- |request|'s url's scheme is not one of 'http' and 'https'" // "- |request|'s url's scheme is not one of 'http' and 'https'"
if (!m_request->url().protocolIsInHTTPFamily()) { if (!m_request->url().protocolIsInHTTPFamily()) {
// "A network error." // "A network error."
performNetworkError(); performNetworkError("Fetch API cannot load " + m_request->url().string() + ". URL scheme must be \"http\" or \"https\" for CORS request.");
return; return;
} }
...@@ -249,13 +249,13 @@ void FetchManager::Loader::performBasicFetch() ...@@ -249,13 +249,13 @@ void FetchManager::Loader::performBasicFetch()
performHTTPFetch(); performHTTPFetch();
} else { } else {
// FIXME: implement other protocols. // FIXME: implement other protocols.
performNetworkError(); performNetworkError("Fetch API cannot load " + m_request->url().string() + ". URL scheme \"" + m_request->url().protocol() + "\" is not supported.");
} }
} }
void FetchManager::Loader::performNetworkError() void FetchManager::Loader::performNetworkError(const String& message)
{ {
failed(); failed(message);
} }
void FetchManager::Loader::performHTTPFetch() void FetchManager::Loader::performHTTPFetch()
...@@ -322,7 +322,7 @@ void FetchManager::Loader::performHTTPFetch() ...@@ -322,7 +322,7 @@ void FetchManager::Loader::performHTTPFetch()
m_loader = ThreadableLoader::create(*m_executionContext, this, request, threadableLoaderOptions, resourceLoaderOptions); m_loader = ThreadableLoader::create(*m_executionContext, this, request, threadableLoaderOptions, resourceLoaderOptions);
} }
void FetchManager::Loader::failed() void FetchManager::Loader::failed(const String& message)
{ {
if (m_failed) if (m_failed)
return; return;
...@@ -331,7 +331,7 @@ void FetchManager::Loader::failed() ...@@ -331,7 +331,7 @@ void FetchManager::Loader::failed()
m_failed = true; m_failed = true;
ScriptState* state = m_resolver->scriptState(); ScriptState* state = m_resolver->scriptState();
ScriptState::Scope scope(state); ScriptState::Scope scope(state);
m_resolver->reject(V8ThrowException::createTypeError(state->isolate(), "Failed to fetch")); m_resolver->reject(V8ThrowException::createTypeError(state->isolate(), message));
notifyFinished(); notifyFinished();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment