Skip to content

Commit

Permalink
Add status code to service worker registration failure message
Browse files Browse the repository at this point in the history
This helps developer understand why the service worker fails to register

Error message:
"Service worker registration failed. Status code: <code>", e.g.
"Service worker registration failed. Status code: 18"

R=lazyboy@chromium.org, rdevlin.cronin@chromium.org

Bug: 1348391
Change-Id: I3a3af5056909593c57fe09f8396599aa2d43cd07
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3805456
Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
Commit-Queue: Richard Zhang <richardzh@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1032240}
  • Loading branch information
Richard Zhang authored and Chromium LUCI CQ committed Aug 6, 2022
1 parent e329343 commit 890efcd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
33 changes: 33 additions & 0 deletions chrome/browser/extensions/service_worker_apitest.cc
Expand Up @@ -442,6 +442,39 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
u"See https://github.com/w3c/ServiceWorker/issues/1356."));
}

// Tests a service worker registration that fails due to the worker script
// synchronously throwing a runtime error.
IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
ServiceWorkerWithRegistrationFailure) {
// The error console only captures errors if the user is in dev mode.
profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true);

constexpr size_t kErrorsExpected = 2u;
ErrorConsole* error_console = ErrorConsole::Get(profile());
ErrorObserver observer(kErrorsExpected, error_console);

const Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("service_worker/worker_based_background/"
"service_worker_registration_failure"),
{.wait_for_renderers = false});

ASSERT_TRUE(extension);
observer.WaitForErrors();
const ErrorList& error_list =
error_console->GetErrorsForExtension(extension->id());
ASSERT_EQ(kErrorsExpected, error_list.size());

std::vector<std::u16string> error_message_list;
for (std::string::size_type i = 0; i < error_list.size(); i++) {
error_message_list.push_back(error_list[i]->message());
}
// status code 15: kErrorScriptEvaluateFailed
EXPECT_THAT(error_message_list,
testing::UnorderedElementsAre(
u"Uncaught Error: lol",
u"Service worker registration failed. Status code: 15"));
}

// Tests that an error is generated if there is a syntax error in the service
// worker script.
IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest, SyntaxError) {
Expand Down
@@ -0,0 +1,9 @@
{
"name": "Service Worker-based background script",
"version": "0.1",
"manifest_version": 3,
"description": "Test a service script that throws error.",
"background": {
"service_worker": "service_worker_background.js"
}
}
@@ -0,0 +1,5 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

throw new Error('lol');
6 changes: 5 additions & 1 deletion extensions/browser/service_worker_task_queue.cc
Expand Up @@ -12,6 +12,7 @@
#include "base/bind.h"
#include "base/containers/contains.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/syslog_logging.h"
#include "content/public/browser/browser_context.h"
Expand Down Expand Up @@ -533,8 +534,11 @@ void ServiceWorkerTaskQueue::DidRegisterServiceWorker(
}

if (!success) {
std::string msg = base::StringPrintf(
"Service worker registration failed. Status code: %d",
static_cast<int>(status_code));
auto error = std::make_unique<ManifestError>(
extension_id, u"Service worker registration failed",
extension_id, base::UTF8ToUTF16(msg),
base::UTF8ToUTF16(manifest_keys::kBackground),
base::UTF8ToUTF16(
BackgroundInfo::GetBackgroundServiceWorkerScript(extension)));
Expand Down

0 comments on commit 890efcd

Please sign in to comment.