Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit f42ea31

Browse files
authored
Add pretty-error page infrastructure (#436)
This adds Miniflare 2's pretty-error page powered by [Youch](https://github.com/poppinss/youch) to Miniflare 3. Unfortunately, due to a bug in `workerd`, errors thrown asynchronously by native APIs don't have `stack`s. This means we can't extract the `stack` trace from dispatching to the user worker by `try`/`catch`. As a stop-gap solution, if the `MF-Experimental-Error-Stack` header exists and is truthy on the response from the user worker, the body will be interpreted as a JSON-error of the form `{ message?: string, name?: string, stack?: string }`. `stack` will be source-mapped if possible. Another issue is that `workerd` gives all service-worker scripts the name "worker.js", so if multiple service-workers are defined, we can't identify which one threw. In this case, we don't display sources in the pretty-error page. Hopefully, we can fix both of these issues in `workerd`. We should be able to reuse most of this infrastructure with `try`/`catch`s too.
1 parent 7a78784 commit f42ea31

File tree

7 files changed

+547
-22
lines changed

7 files changed

+547
-22
lines changed

package-lock.json

Lines changed: 136 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tre/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ parameter in module format Workers.
223223
must also be defined. Note the first module must be the entrypoint and have
224224
type `"ESModule"`.
225225

226+
- `modulesRoot?: string`
227+
228+
If `modules` is set to an array, modules' "name"s will be their `path`s
229+
relative to this value. This ensures file paths in stack traces are correct.
230+
226231
<!-- prettier-ignore-start -->
227232
<!-- (for disabling `;` insertion in `js` code block) -->
228233

packages/tre/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
"glob-to-regexp": "^0.4.1",
3636
"http-cache-semantics": "^4.1.0",
3737
"kleur": "^4.1.5",
38+
"source-map": "^0.7.4",
3839
"stoppable": "^1.1.0",
3940
"undici": "^5.12.0",
4041
"workerd": "^1.20221111.5",
4142
"ws": "^8.11.0",
43+
"youch": "^3.2.2",
4244
"zod": "^3.18.0"
4345
},
4446
"devDependencies": {

packages/tre/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ import {
2727
maybeGetSitesManifestModule,
2828
normaliseDurableObject,
2929
} from "./plugins";
30-
import { HEADER_CUSTOM_SERVICE, getUserServiceName } from "./plugins/core";
30+
import {
31+
HEADER_CUSTOM_SERVICE,
32+
SourceOptions,
33+
getUserServiceName,
34+
handlePrettyErrorRequest,
35+
} from "./plugins/core";
3136
import {
3237
Config,
3338
Runtime,
@@ -345,6 +350,11 @@ export class Miniflare {
345350
request,
346351
customService
347352
);
353+
} else if (url.pathname === "/core/error") {
354+
const workerSrcOpts = this.#workerOpts.map<SourceOptions>(
355+
({ core }) => core
356+
);
357+
response = await handlePrettyErrorRequest(workerSrcOpts, request);
348358
} else {
349359
// TODO: check for proxying/outbound fetch header first (with plans for fetch mocking)
350360
response = await this.#handleLoopbackPlugins(request, url);

0 commit comments

Comments
 (0)