matrix-org / matrix-org/matrix-viewer

Explore replacing our `RethrownError` with native canonical `new Error(message, { cause: rethrownError })`

Open
#63 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

dev-notes T-Enhancement
Dominant language
JavaScript
Stars
84
Forks
10
PR merge metrics
No merged PRs in 30d

Description

Looking at MDN, I saw that `new Error(message, { cause: rethrownError })` seems canonical now ([as of Chrome 93 and Node.js 16.9.0](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#browser_compatibility)). So we could possibly use that instead of our custom [`RethrownError`](https://github.com/matrix-org/matrix-public-archive/blob/fd00fec6f1f91ea55fe59b05f0658318c2738f42/server/lib/rethrown-error.js). I wonder if it has any special formatting or context?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#parameters

> `options` (Optional)
> An object that has the following properties:
>
> - `cause` (Optional)
> A property indicating the specific cause of the error. When catching and re-throwing an error with a more-specific or useful error message, this property can be used to pass the original error.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#rethrowing_an_error_with_a_cause

> ### Rethrowing an error with a cause
>
> It is sometimes useful to catch an error and re-throw it with a new message. In this case you should pass the original error into the constructor for the new Error, as shown.
> ```js
> try {
> frameworkThatCanThrow();
> } catch (err) {
> throw new Error('New error message', { cause: err });
> }
> ```

## What does the output of our custom `RethrownError` look like in different scenarios?

```
RethrownError: Failed to render Hydrogen to string. In order to reproduce, feed in these arguments into `renderHydrogenToString(...)`:
renderToString arguments: {"searchTerm":"foobar","config":{"basePath":"http://192.168.1.151:3050"}}
at renderHydrogenToString (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:163:11)
--- Original Error ---
RethrownError: Child process exited with code 1
at assembleErrorAfterChildExitsWithErrors (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:49:29)
--- Original Error ---
TypeError: t.header is not a function
at RoomDirectoryView.render (matrix-public-archive\shared\views\RoomDirectoryView.js:54:11)
at RoomDirectoryView.mount (C:\Users\MLM\Documents\GitHub\element\hydrogen-web\target\lib-build\hydrogen.cjs.js:3834:25)
at mountHydrogen (4-room-directory-vm-render-script.js:28:35)
at 4-room-directory-vm-render-script.js:34:1
at Script.runInContext (node:vm:139:12)
at _renderHydrogenToStringUnsafe (matrix-public-archive\server\hydrogen-render\3-render-hydrogen-to-string-unsafe.js:89:41)
at async process. (matrix-public-archive\server\hydrogen-render\2-render-hydrogen-to-string-fork-script.js:66:27)
```

```
RethrownError: Failed to render Hydrogen to string. In order to reproduce, feed in these arguments into `renderHydrogenToString(...)`:
renderToString arguments: {"fromTimestamp":1644278400000,"roomData":{"id":"!HBehERstyQBxyJDLfR:my.synapse.server","name":"test-room1"},"events":[...],"stateEventMap":{...}}
at renderHydrogenToString (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:162:11)
--- Original Error ---
RethrownError: Timed out while rendering Hydrogen to string so we aborted the child process after 5000ms. Any child errors? (1)
at ChildProcess. (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:141:13)
--- Original Error ---
RethrownError: Child process exited with code (not set yet)
at assembleErrorAfterChildExitsWithErrors (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:48:29)
--- Original Error ---
RethrownError: unhandledRejection in child process
at process. (matrix-public-archive\server\hydrogen-render\2-render-hydrogen-to-string-fork-script.js:54:24)
--- Original Error ---
Error: fake child error
at matrix-public-archive\server\hydrogen-render\2-render-hydrogen-to-string-fork-script.js:62:11
at new Promise ()
at process. (matrix-public-archive\server\hydrogen-render\2-render-hydrogen-to-string-fork-script.js:61:3)
at process.emit (node:events:520:28)
at emit (node:internal/child_process:938:14)
at processTicksAndRejections (node:internal/process/task_queues:84:21)
```

```
RethrownError: Failed to render Hydrogen to string. In order to reproduce, feed in these arguments into `renderHydrogenToString(...)`:
renderToString arguments: {"fromTimestamp":1644278400000,"roomData":{"id":"!HBehERstyQBxyJDLfR:my.synapse.server","name":"test-room1"},"events":[...],"stateEventMap":{...}
at renderHydrogenToString (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:162:11)
--- Original Error ---
RethrownError: No HTML sent from child process to render Hydrogen. Any child errors? (0)
at renderHydrogenToString (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:154:13)
--- Original Error ---
RethrownError: Child process exited with code 0
at assembleErrorAfterChildExitsWithErrors (matrix-public-archive\server\hydrogen-render\1-render-hydrogen-to-string.js:48:29)
--- Original Error ---
No child errors
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading server/lib/rethrown-error.js and the rendering entry points in server/hydrogen-render/1-render-hydrogen-to-string.js, especially the reported throw locations. Compare native Error cause behavior with the custom output shown in the issue across these rendering and child-process scenarios. Done means establishing whether native errors preserve the required context and formatting, and documenting the replacement scope or compatibility blockers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.