@tryghost/mw-error-handler 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -0
- package/package.json +2 -1
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# MW Error Handler
|
|
2
|
+
|
|
3
|
+
Express middleware helpers for Ghost-style API error handling.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
`npm install @tryghost/mw-error-handler --save`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`yarn add @tryghost/mw-error-handler`
|
|
12
|
+
|
|
13
|
+
## Purpose
|
|
14
|
+
|
|
15
|
+
`@tryghost/mw-error-handler` normalizes thrown errors into `@tryghost/errors` types, applies appropriate cache-control headers for error responses, and exposes ready-to-use middleware stacks for JSON and HTML error flows.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const express = require('express');
|
|
21
|
+
const sentry = require('./sentry');
|
|
22
|
+
const errorHandler = require('@tryghost/mw-error-handler');
|
|
23
|
+
|
|
24
|
+
const app = express();
|
|
25
|
+
|
|
26
|
+
app.get('/api/example', (req, res) => {
|
|
27
|
+
throw new Error('Boom');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
app.use(errorHandler.resourceNotFound);
|
|
31
|
+
app.use(...errorHandler.handleJSONResponse(sentry));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
- `prepareError(err, req, res, next)`: converts unknown errors to `@tryghost/errors` instances.
|
|
37
|
+
- `prepareStack(err, req, res, next)`: sanitizes stack traces for user output.
|
|
38
|
+
- `jsonErrorRenderer(err, req, res, next)`: renders `{ errors: [...] }` JSON payloads.
|
|
39
|
+
- `prepareErrorCacheControl([cacheControl])`: middleware factory for error cache-control headers.
|
|
40
|
+
- `resourceNotFound(req, res, next)`: creates a not-found error (with API version validation support).
|
|
41
|
+
- `pageNotFound(req, res, next)`: creates a not-found page error.
|
|
42
|
+
- `handleJSONResponse(sentry)`: returns middleware sequence for JSON error responses.
|
|
43
|
+
- `handleHTMLResponse(sentry)`: returns middleware sequence for HTML error responses.
|
|
44
|
+
|
|
45
|
+
# Copyright & License
|
|
46
|
+
|
|
47
|
+
Copyright (c) 2013-2026 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/package.json
CHANGED