You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/error-plugin.md
+91-29Lines changed: 91 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,13 @@ description: Learn about the Error plugin for error handling in Merkur
6
6
7
7
# Error plugin
8
8
9
-
A plugin for [Merkur](https://merkur.js.org/) is tiny extensible javascript library for front-end microservices.
9
+
The error plugin adds semi-automatic error handling to your Merkur widget. It hooks into the widget lifecycle (`load`, `mount`, `update`) to catch thrown errors, saves the error state on the widget, and emits an event so the rest of the application can react.
10
10
11
-
12
-
@merkur/plugin-error adds semi-automatic error handling to your Merkur widget:
13
-
14
-
* Return a custom HTTP status based on thrown error
15
-
* Render valid JSON with error code and message
16
-
* Render an error page
17
-
* Run arbitrary code on error (e.g. to log/report the error)
18
-
* Catch unhandled promise errors
19
-
* Provide error class to extend
11
+
- Returns a custom HTTP status based on the thrown error
12
+
- Renders valid JSON with the error code and message via the `info` lifecycle
13
+
- Renders an error page (re-runs `mount`/`update` after capturing the error)
14
+
- Runs arbitrary code on error via the `ERROR_EVENTS.ERROR` event (e.g. logging)
15
+
- Exposes `GenericError` and `ExtensibleError` base classes for custom errors
20
16
21
17
22
18
## Installation
@@ -61,17 +57,28 @@ Set HTTP status in widget API response:
61
57
62
58
## Operation
63
59
64
-
When an error is thrown, the plugin does the following:
60
+
The plugin hooks four lifecycle methods: `load`, `mount`, `update`, and `info`.
61
+
62
+
**`load` hook** — If `widget.error.status` is already set, `load` is skipped entirely and returns `{}`. Otherwise the original `load` is called; if it throws, the error is captured via `setErrorInfo` and an empty result is returned.
63
+
64
+
**`mount` / `update` hooks** — Delegate to `renderContent`:
65
+
- If `widget.error.status` is already set when the hook runs, the original method is attempted once more (giving the view a chance to render its error state). If that also throws, an empty string is returned.
66
+
- If no error is set and the original method throws, the error is captured and the hook immediately retries — this time the error state is set so the view can render accordingly.
67
+
68
+
**`info` hook** — Merges `widget.error` into the info object returned to the host application:
The core rendering helper used internally by the `mount` and `update` hooks. It can also be used directly when you need the same error-resilient render logic in a custom lifecycle method.
150
+
151
+
**Parameters:**
152
+
- `widget` - The widget instance
153
+
- `method` - An async function to call (e.g. the original `mount`/`update`)
154
+
- `properties` - Array of arguments to pass to `method`
155
+
156
+
**Behaviour:**
157
+
- If `widget.error.status` is already set: calls `method` once and returns its result, or returns `''` if it throws (without overwriting the existing error).
158
+
- If `widget.error.status` is not set: calls `method`; if it throws, captures the error with `setErrorInfo` and recursively retries (now with error state set).
- `params` — object with any extra data; `params.status` sets the HTTP status (defaults to `500`) and is removed from `params` itself
143
182
144
-
`GenericError` class carries `status` param among with other params. This way error plugin can respond with adequate HTTP status and also include data for encountered error.
183
+
**`params` getter** — returns the extra parameters (excluding `status`):
console.log(error.params.url); // '/api/data' — stored as widget.error.url by setErrorInfo
198
+
```
199
+
200
+
### `ExtensibleError`
201
+
202
+
`ExtensibleError` is the abstract base class that fixes Babel-related issues with extending the native `Error` class. Use it when you need a custom error hierarchy beyond `GenericError`:
0 commit comments