Skip to content

Commit

Permalink
Add pretty-error page infrastructure (#436)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrbbot authored Nov 18, 2022
1 parent 7a78784 commit f42ea31
Show file tree
Hide file tree
Showing 7 changed files with 547 additions and 22 deletions.
139 changes: 136 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/tre/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ parameter in module format Workers.
must also be defined. Note the first module must be the entrypoint and have
type `"ESModule"`.

- `modulesRoot?: string`

If `modules` is set to an array, modules' "name"s will be their `path`s
relative to this value. This ensures file paths in stack traces are correct.

<!-- prettier-ignore-start -->
<!-- (for disabling `;` insertion in `js` code block) -->

Expand Down
2 changes: 2 additions & 0 deletions packages/tre/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
"glob-to-regexp": "^0.4.1",
"http-cache-semantics": "^4.1.0",
"kleur": "^4.1.5",
"source-map": "^0.7.4",
"stoppable": "^1.1.0",
"undici": "^5.12.0",
"workerd": "^1.20221111.5",
"ws": "^8.11.0",
"youch": "^3.2.2",
"zod": "^3.18.0"
},
"devDependencies": {
Expand Down
12 changes: 11 additions & 1 deletion packages/tre/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
maybeGetSitesManifestModule,
normaliseDurableObject,
} from "./plugins";
import { HEADER_CUSTOM_SERVICE, getUserServiceName } from "./plugins/core";
import {
HEADER_CUSTOM_SERVICE,
SourceOptions,
getUserServiceName,
handlePrettyErrorRequest,
} from "./plugins/core";
import {
Config,
Runtime,
Expand Down Expand Up @@ -345,6 +350,11 @@ export class Miniflare {
request,
customService
);
} else if (url.pathname === "/core/error") {
const workerSrcOpts = this.#workerOpts.map<SourceOptions>(
({ core }) => core
);
response = await handlePrettyErrorRequest(workerSrcOpts, request);
} else {
// TODO: check for proxying/outbound fetch header first (with plans for fetch mocking)
response = await this.#handleLoopbackPlugins(request, url);
Expand Down
Loading

0 comments on commit f42ea31

Please sign in to comment.