Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion php-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import {
createPhpBridgeChannel,
createShellChannel,
formatErrorDetail,
} from "./src/shared/protocol.js";
import {
bootstrapNextcloud,
Expand All @@ -18,7 +19,6 @@ import {
isEmscriptenNetworkError,
isFatalWasmError,
isSafeToReplay,
formatErrorDetail,
createSnapshotManager,
requestPathname,
} from "./src/runtime/crash-recovery.js";
Expand Down
23 changes: 0 additions & 23 deletions src/runtime/crash-recovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,6 @@ export function requestPathname(request) {
}
}

/**
* Format an error into a human-readable string for display/logging.
*/
export function formatErrorDetail(error) {
if (!error) {
return "Unknown error";
}

if (typeof error === "string") {
return error;
}

if (error instanceof Error) {
return String(error.stack || error.message || error);
}

try {
return JSON.stringify(error, null, 2);
} catch {
return String(error);
}
}

/**
* Create a state snapshot manager for crash recovery.
*
Expand Down
29 changes: 29 additions & 0 deletions src/shared/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,32 @@ export function createPhpBridgeChannel(scopeId) {
export function createWorkerRequestId() {
return globalThis.crypto.randomUUID();
}

/**
* Format an error into a human-readable string for display/logging.
*/
export function formatErrorDetail(error) {
if (!error) {
return "Unknown error";
}

if (typeof error === "string") {
return error;
}

if (error instanceof Error) {
// Chromium's stack starts with "Name: message"; Firefox and Safari list
// only the frames, so the message would be lost.
const head = `${error.name}: ${error.message}`;
if (!error.stack || error.stack.startsWith(head)) {
return String(error.stack || head);
}
return `${head}\n${error.stack}`.trim();
}

try {
return JSON.stringify(error, null, 2);
} catch {
return String(error);
}
}
6 changes: 3 additions & 3 deletions src/shell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
hasBlueprintUrlOverride,
resolveRemoteUrl,
} from "../shared/paths.js";
import { createShellChannel } from "../shared/protocol.js";
import { createShellChannel, formatErrorDetail } from "../shared/protocol.js";
import {
createServiceWorkerUnsupportedError,
isServiceWorkerSupported,
Expand Down Expand Up @@ -916,7 +916,7 @@ els.importInput.addEventListener("change", async () => {
await importPayload(file);
appendLog(`Imported configuration from ${file.name}`);
} catch (error) {
appendLog(String(error?.stack || error?.message || error), true);
appendLog(formatErrorDetail(error), true);
} finally {
els.importInput.value = "";
}
Expand Down Expand Up @@ -947,6 +947,6 @@ els.reset.addEventListener("click", () => {

main().catch((error) => {
setUiLocked(false);
appendLog(String(error?.stack || error?.message || error), true);
appendLog(formatErrorDetail(error), true);
captureException(error, { source: "shell-main" });
});
6 changes: 3 additions & 3 deletions sw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BUILD_VERSION } from "./src/generated/build-version.js";
import { createPhpBridgeChannel, createWorkerRequestId } from "./src/shared/protocol.js";
import { createPhpBridgeChannel, createWorkerRequestId, formatErrorDetail } from "./src/shared/protocol.js";
import {
buildSandboxedIframeCompatibilityScript,
consumeSandboxCompatibility,
Expand Down Expand Up @@ -662,7 +662,7 @@ self.addEventListener("fetch", (event) => {
serializedRequest: await buildForwardedRequest(event.request, forwardedUrl, bufferedBody),
runtimeId,
scopeId,
}).catch((error) => buildErrorResponse(String(error?.stack || error?.message || error)));
}).catch((error) => buildErrorResponse(formatErrorDetail(error)));

if (isCacheableAssetResponse(requestPath, fresh)) {
cache.put(url.toString(), fresh.clone()).catch(() => {});
Expand All @@ -675,7 +675,7 @@ self.addEventListener("fetch", (event) => {
serializedRequest: await buildForwardedRequest(event.request, forwardedUrl, bufferedBody),
runtimeId,
scopeId,
}).catch((error) => buildErrorResponse(String(error?.stack || error?.message || error)));
}).catch((error) => buildErrorResponse(formatErrorDetail(error)));

const locationScopedResponse = rewriteScopedLocation(response, {
origin: url.origin,
Expand Down
23 changes: 23 additions & 0 deletions tests/shared-protocol.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from "node:assert/strict";
import { test } from "node:test";

import { formatErrorDetail } from "../src/shared/protocol.js";

test("formatErrorDetail keeps the error message when the stack omits it", () => {
// Firefox and Safari: the stack has only frames, no "Name: message" line.
const firefox = new Error("needs what this installation lacks");
firefox.name = "PHPExecutionFailureError";
firefox.stack = "run@http://localhost/dist/php-worker.bundle.js:1:1\n";
assert.equal(
formatErrorDetail(firefox),
"PHPExecutionFailureError: needs what this installation lacks\nrun@http://localhost/dist/php-worker.bundle.js:1:1",
);

// Chromium: the stack already starts with "Name: message".
const chromium = new Error("boom");
assert.equal(formatErrorDetail(chromium), chromium.stack);

assert.equal(formatErrorDetail("plain"), "plain");
assert.equal(formatErrorDetail(null), "Unknown error");
assert.equal(formatErrorDetail({ a: 1 }), '{\n "a": 1\n}');
});
Loading