@visulima/error-handler 1.0.0
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/CHANGELOG.md +9 -0
- package/LICENSE.md +193 -0
- package/README.md +388 -0
- package/dist/error-handler/html-error-handler.d.ts +16 -0
- package/dist/error-handler/html-error-handler.js +63 -0
- package/dist/error-handler/json-error-handler.d.ts +17 -0
- package/dist/error-handler/json-error-handler.js +31 -0
- package/dist/error-handler/jsonapi-error-handler.d.ts +6 -0
- package/dist/error-handler/jsonapi-error-handler.js +46 -0
- package/dist/error-handler/jsonp-error-handler.d.ts +18 -0
- package/dist/error-handler/jsonp-error-handler.js +36 -0
- package/dist/error-handler/problem-error-handler.d.ts +6 -0
- package/dist/error-handler/problem-error-handler.js +53 -0
- package/dist/error-handler/text-error-handler.d.ts +16 -0
- package/dist/error-handler/text-error-handler.js +28 -0
- package/dist/error-handler/xml-error-handler.d.ts +29 -0
- package/dist/error-handler/xml-error-handler.js +45 -0
- package/dist/handler/cli-handler.d.ts +30 -0
- package/dist/handler/cli-handler.js +173 -0
- package/dist/handler/http/fetch-handler.d.ts +11 -0
- package/dist/handler/http/fetch-handler.js +267 -0
- package/dist/handler/http/node-handler.d.ts +11 -0
- package/dist/handler/http/node-handler.js +17 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +8 -0
- package/dist/packem_shared/add-status-code-to-response-DdfxTQsK.js +17 -0
- package/dist/packem_shared/createNegotiatedErrorHandler-Cd7yj67Z.js +52 -0
- package/dist/packem_shared/send-json-o-t1rNfw.js +8 -0
- package/dist/packem_shared/set-error-headers-B6ZeX5k4.js +10 -0
- package/dist/packem_shared/types.d-DLy8CCca.d.ts +26 -0
- package/package.json +151 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
## @visulima/error-handler 1.0.0 (2025-09-10)
|
|
2
|
+
|
|
3
|
+
### Features
|
|
4
|
+
|
|
5
|
+
* **error-handler:** Added new error-handler package with various error handling utilities. ([585cae3](https://github.com/visulima/visulima/commit/585cae3f680cce87117936dafbbe0b0dad328725))
|
|
6
|
+
|
|
7
|
+
### Miscellaneous Chores
|
|
8
|
+
|
|
9
|
+
* **error-handler:** add Prettier configuration and ignore file, remove outdated test file ([a715d18](https://github.com/visulima/visulima/commit/a715d18d39b95eab51b69908e323ff332f78160d))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 visulima
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
<!-- DEPENDENCIES -->
|
|
24
|
+
|
|
25
|
+
# Licenses of bundled dependencies
|
|
26
|
+
The published @visulima/error-handler artifact additionally contains code with the following licenses:
|
|
27
|
+
ISC, MIT
|
|
28
|
+
|
|
29
|
+
# Bundled dependencies:
|
|
30
|
+
## depd
|
|
31
|
+
License: MIT
|
|
32
|
+
By: Douglas Christopher Wilson
|
|
33
|
+
Repository: dougwilson/nodejs-depd
|
|
34
|
+
|
|
35
|
+
> (The MIT License)
|
|
36
|
+
>
|
|
37
|
+
> Copyright (c) 2014-2018 Douglas Christopher Wilson
|
|
38
|
+
>
|
|
39
|
+
> Permission is hereby granted, free of charge, to any person obtaining
|
|
40
|
+
> a copy of this software and associated documentation files (the
|
|
41
|
+
> 'Software'), to deal in the Software without restriction, including
|
|
42
|
+
> without limitation the rights to use, copy, modify, merge, publish,
|
|
43
|
+
> distribute, sublicense, and/or sell copies of the Software, and to
|
|
44
|
+
> permit persons to whom the Software is furnished to do so, subject to
|
|
45
|
+
> the following conditions:
|
|
46
|
+
>
|
|
47
|
+
> The above copyright notice and this permission notice shall be
|
|
48
|
+
> included in all copies or substantial portions of the Software.
|
|
49
|
+
>
|
|
50
|
+
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
51
|
+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
52
|
+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
53
|
+
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
54
|
+
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
55
|
+
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
56
|
+
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
57
|
+
|
|
58
|
+
---------------------------------------
|
|
59
|
+
|
|
60
|
+
## http-errors
|
|
61
|
+
License: MIT
|
|
62
|
+
By: Jonathan Ong, Alan Plum, Douglas Christopher Wilson
|
|
63
|
+
Repository: jshttp/http-errors
|
|
64
|
+
|
|
65
|
+
> The MIT License (MIT)
|
|
66
|
+
>
|
|
67
|
+
> Copyright (c) 2014 Jonathan Ong me@jongleberry.com
|
|
68
|
+
> Copyright (c) 2016 Douglas Christopher Wilson doug@somethingdoug.com
|
|
69
|
+
>
|
|
70
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
71
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
72
|
+
> in the Software without restriction, including without limitation the rights
|
|
73
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
74
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
75
|
+
> furnished to do so, subject to the following conditions:
|
|
76
|
+
>
|
|
77
|
+
> The above copyright notice and this permission notice shall be included in
|
|
78
|
+
> all copies or substantial portions of the Software.
|
|
79
|
+
>
|
|
80
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
81
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
82
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
83
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
84
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
85
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
86
|
+
> THE SOFTWARE.
|
|
87
|
+
|
|
88
|
+
---------------------------------------
|
|
89
|
+
|
|
90
|
+
## inherits
|
|
91
|
+
License: ISC
|
|
92
|
+
Repository: git://github.com/isaacs/inherits
|
|
93
|
+
|
|
94
|
+
> The ISC License
|
|
95
|
+
>
|
|
96
|
+
> Copyright (c) Isaac Z. Schlueter
|
|
97
|
+
>
|
|
98
|
+
> Permission to use, copy, modify, and/or distribute this software for any
|
|
99
|
+
> purpose with or without fee is hereby granted, provided that the above
|
|
100
|
+
> copyright notice and this permission notice appear in all copies.
|
|
101
|
+
>
|
|
102
|
+
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
103
|
+
> REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
104
|
+
> FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
105
|
+
> INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
106
|
+
> LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
107
|
+
> OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
108
|
+
> PERFORMANCE OF THIS SOFTWARE.
|
|
109
|
+
|
|
110
|
+
---------------------------------------
|
|
111
|
+
|
|
112
|
+
## setprototypeof
|
|
113
|
+
License: ISC
|
|
114
|
+
By: Wes Todd
|
|
115
|
+
Repository: https://github.com/wesleytodd/setprototypeof.git
|
|
116
|
+
|
|
117
|
+
> Copyright (c) 2015, Wes Todd
|
|
118
|
+
>
|
|
119
|
+
> Permission to use, copy, modify, and/or distribute this software for any
|
|
120
|
+
> purpose with or without fee is hereby granted, provided that the above
|
|
121
|
+
> copyright notice and this permission notice appear in all copies.
|
|
122
|
+
>
|
|
123
|
+
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
124
|
+
> WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
125
|
+
> MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
126
|
+
> SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
127
|
+
> WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
128
|
+
> OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
129
|
+
> CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
130
|
+
|
|
131
|
+
---------------------------------------
|
|
132
|
+
|
|
133
|
+
## statuses
|
|
134
|
+
License: MIT
|
|
135
|
+
By: Douglas Christopher Wilson, Jonathan Ong
|
|
136
|
+
Repository: jshttp/statuses
|
|
137
|
+
|
|
138
|
+
> The MIT License (MIT)
|
|
139
|
+
>
|
|
140
|
+
> Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
|
|
141
|
+
> Copyright (c) 2016 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
142
|
+
>
|
|
143
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
144
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
145
|
+
> in the Software without restriction, including without limitation the rights
|
|
146
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
147
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
148
|
+
> furnished to do so, subject to the following conditions:
|
|
149
|
+
>
|
|
150
|
+
> The above copyright notice and this permission notice shall be included in
|
|
151
|
+
> all copies or substantial portions of the Software.
|
|
152
|
+
>
|
|
153
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
154
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
155
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
156
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
157
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
158
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
159
|
+
> THE SOFTWARE.
|
|
160
|
+
|
|
161
|
+
---------------------------------------
|
|
162
|
+
|
|
163
|
+
## toidentifier
|
|
164
|
+
License: MIT
|
|
165
|
+
By: Douglas Christopher Wilson, Nick Baugh
|
|
166
|
+
Repository: component/toidentifier
|
|
167
|
+
|
|
168
|
+
> MIT License
|
|
169
|
+
>
|
|
170
|
+
> Copyright (c) 2016 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
171
|
+
>
|
|
172
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
173
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
174
|
+
> in the Software without restriction, including without limitation the rights
|
|
175
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
176
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
177
|
+
> furnished to do so, subject to the following conditions:
|
|
178
|
+
>
|
|
179
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
180
|
+
> copies or substantial portions of the Software.
|
|
181
|
+
>
|
|
182
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
183
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
184
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
185
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
186
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
187
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
188
|
+
> SOFTWARE.
|
|
189
|
+
|
|
190
|
+
<!-- /DEPENDENCIES -->
|
|
191
|
+
|
|
192
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
193
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h3>visulima error-handler</h3>
|
|
3
|
+
<p>
|
|
4
|
+
Error handlers for use in development and production environments.
|
|
5
|
+
</p>
|
|
6
|
+
</div>
|
|
7
|
+
|
|
8
|
+
<br />
|
|
9
|
+
|
|
10
|
+
<div align="center">
|
|
11
|
+
|
|
12
|
+
[![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<p>
|
|
20
|
+
<sup>
|
|
21
|
+
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
|
|
22
|
+
</sup>
|
|
23
|
+
</p>
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @visulima/error-handler
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
yarn add @visulima/error-handler
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
pnpm add @visulima/error-handler
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- **Content Negotiation** - Automatically serves HTML, JSON, Problem JSON, or JSON:API based on `Accept` header
|
|
45
|
+
- **Framework Agnostic** - Works with Node.js HTTP, Express, Fastify, Koa, Hono, and any Fetch-based runtime
|
|
46
|
+
- **Production Ready** - Configurable error pages, trace control, and custom handlers
|
|
47
|
+
- **TypeScript Support** - Full type safety with comprehensive TypeScript definitions
|
|
48
|
+
- **Extensible** - Custom error handlers via regex matching on Accept headers
|
|
49
|
+
- **CSP Support** - Built-in Content Security Policy nonce support for inline styles
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
### Node.js HTTP Server
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { createServer } from "node:http";
|
|
57
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
58
|
+
|
|
59
|
+
const server = createServer(async (req, res) => {
|
|
60
|
+
try {
|
|
61
|
+
// your app logic...
|
|
62
|
+
throw new Error("Boom!");
|
|
63
|
+
} catch (error) {
|
|
64
|
+
const handler = await httpHandler(error as Error, {
|
|
65
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
66
|
+
});
|
|
67
|
+
return handler(req, res);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
server.listen(3000);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Express Setup
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import express from "express";
|
|
78
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
79
|
+
|
|
80
|
+
const app = express();
|
|
81
|
+
app.use(express.json());
|
|
82
|
+
|
|
83
|
+
app.get("/", async (req, res) => {
|
|
84
|
+
try {
|
|
85
|
+
throw new Error("Example error");
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const handler = await httpHandler(error as Error, {
|
|
88
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
89
|
+
});
|
|
90
|
+
return handler(req, res);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
app.listen(3000);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Hono (Fetch Runtime)
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { serve } from "@hono/node-server";
|
|
101
|
+
import { Hono } from "hono";
|
|
102
|
+
import fetchHandler from "@visulima/error-handler/handler/fetch";
|
|
103
|
+
|
|
104
|
+
const app = new Hono();
|
|
105
|
+
|
|
106
|
+
app.get("/", (c) => c.text("OK"));
|
|
107
|
+
app.get("/error", () => {
|
|
108
|
+
throw new Error("Boom from Hono");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
app.onError(async (error, c) => {
|
|
112
|
+
const handler = await fetchHandler(error as Error, {
|
|
113
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
114
|
+
});
|
|
115
|
+
return handler(c.req.raw);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
serve({ fetch: app.fetch, port: 3000 });
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Fetch-based Runtimes
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
// Cloudflare Workers
|
|
125
|
+
import fetchHandler from "@visulima/error-handler/handler/fetch";
|
|
126
|
+
|
|
127
|
+
export default {
|
|
128
|
+
async fetch(request: Request): Promise<Response> {
|
|
129
|
+
try {
|
|
130
|
+
throw new Error("Boom");
|
|
131
|
+
} catch (error) {
|
|
132
|
+
const handler = await fetchHandler(error as Error, {
|
|
133
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
134
|
+
});
|
|
135
|
+
return handler(request);
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
// Deno
|
|
143
|
+
import fetchHandler from "@visulima/error-handler/handler/fetch";
|
|
144
|
+
|
|
145
|
+
Deno.serve(async (request: Request) => {
|
|
146
|
+
try {
|
|
147
|
+
throw new Error("Boom");
|
|
148
|
+
} catch (error) {
|
|
149
|
+
const handler = await fetchHandler(error as Error, {
|
|
150
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
151
|
+
});
|
|
152
|
+
return handler(request);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Content Negotiation
|
|
158
|
+
|
|
159
|
+
The error handler automatically serves different content types based on the `Accept` header:
|
|
160
|
+
|
|
161
|
+
- `text/html` → HTML error page
|
|
162
|
+
- `application/problem+json` → Problem JSON (RFC 7807)
|
|
163
|
+
- `application/json` → Simple JSON response
|
|
164
|
+
- `application/vnd.api+json` → JSON:API format
|
|
165
|
+
- `text/plain` → Plain text
|
|
166
|
+
- `application/javascript` → JavaScript error throw
|
|
167
|
+
|
|
168
|
+
### Custom Handlers
|
|
169
|
+
|
|
170
|
+
Add custom handlers for specific content types:
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
174
|
+
|
|
175
|
+
const handler = await httpHandler(error, {
|
|
176
|
+
extraHandlers: [
|
|
177
|
+
{
|
|
178
|
+
regex: /application\/yaml/u,
|
|
179
|
+
handler: (error, req, res) => {
|
|
180
|
+
res.setHeader("content-type", "application/yaml");
|
|
181
|
+
res.end(`error: ${error.message}`);
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## API
|
|
189
|
+
|
|
190
|
+
### httpHandler(error, options?) => Promise<(req, res) => Promise<void>>
|
|
191
|
+
|
|
192
|
+
Node.js HTTP handler for Express, Connect, Fastify, Koa, and similar frameworks.
|
|
193
|
+
|
|
194
|
+
**Parameters:**
|
|
195
|
+
- `error: Error` - The error to handle
|
|
196
|
+
- `options?: HtmlErrorHandlerOptions & { showTrace?: boolean; extraHandlers?: ErrorHandlers }`
|
|
197
|
+
|
|
198
|
+
**Options:**
|
|
199
|
+
- `showTrace?: boolean` - Include stack trace in responses (default: `true`)
|
|
200
|
+
- `extraHandlers?: ErrorHandlers` - Custom handlers for specific Accept headers
|
|
201
|
+
- `errorPage?: string | ((params) => string | Promise<string>)` - Custom HTML error page
|
|
202
|
+
- `cspNonce?: string` - Content Security Policy nonce for inline styles
|
|
203
|
+
- `onError?: (error, request, response) => void | Promise<void>` - Callback for custom error logging
|
|
204
|
+
|
|
205
|
+
### fetchHandler(error, options?) => Promise<(request) => Promise<Response>>
|
|
206
|
+
|
|
207
|
+
Fetch API handler for Cloudflare Workers, Deno, Bun, and other Fetch-based runtimes.
|
|
208
|
+
|
|
209
|
+
**Parameters:**
|
|
210
|
+
- `error: Error` - The error to handle
|
|
211
|
+
- `options?: HtmlErrorHandlerOptions & { showTrace?: boolean; extraHandlers?: FetchErrorHandlers }`
|
|
212
|
+
|
|
213
|
+
**Options:**
|
|
214
|
+
- Same as `httpHandler` but uses `FetchErrorHandlers` for custom handlers
|
|
215
|
+
- `onError?: (error, request, response) => void | Promise<void>` - Callback for custom error logging
|
|
216
|
+
|
|
217
|
+
### Error Handler Types
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
type ErrorHandlers = {
|
|
221
|
+
handler: ErrorHandler;
|
|
222
|
+
regex: RegExp;
|
|
223
|
+
}[];
|
|
224
|
+
|
|
225
|
+
type FetchErrorHandlers = {
|
|
226
|
+
handler: FetchErrorHandler;
|
|
227
|
+
regex: RegExp;
|
|
228
|
+
}[];
|
|
229
|
+
|
|
230
|
+
type HtmlErrorHandlerOptions = {
|
|
231
|
+
errorPage?: string | ((params: {
|
|
232
|
+
error: Error;
|
|
233
|
+
request: IncomingMessage;
|
|
234
|
+
response: ServerResponse;
|
|
235
|
+
reasonPhrase: string;
|
|
236
|
+
statusCode: number;
|
|
237
|
+
}) => string | Promise<string>);
|
|
238
|
+
cspNonce?: string;
|
|
239
|
+
onError?: (error: Error, request: IncomingMessage, response: ServerResponse) => void | Promise<void>;
|
|
240
|
+
};
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Content Negotiation Examples
|
|
244
|
+
|
|
245
|
+
### Basic Usage
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import { createServer } from "node:http";
|
|
249
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
250
|
+
|
|
251
|
+
const server = createServer(async (req, res) => {
|
|
252
|
+
try {
|
|
253
|
+
throw new Error("Test error");
|
|
254
|
+
} catch (error) {
|
|
255
|
+
const handler = await httpHandler(error as Error, {
|
|
256
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
257
|
+
});
|
|
258
|
+
return handler(req, res);
|
|
259
|
+
}
|
|
260
|
+
}).listen(3000);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### With Custom HTML Error Page
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
267
|
+
|
|
268
|
+
const handler = await httpHandler(error, {
|
|
269
|
+
errorPage: ({ error, statusCode }) =>
|
|
270
|
+
`<!DOCTYPE html>
|
|
271
|
+
<html>
|
|
272
|
+
<head><title>Error ${statusCode}</title></head>
|
|
273
|
+
<body>
|
|
274
|
+
<h1>Error ${statusCode}</h1>
|
|
275
|
+
<p>${error.message}</p>
|
|
276
|
+
</body>
|
|
277
|
+
</html>`,
|
|
278
|
+
showTrace: false
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### With CSP Nonce Support
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
286
|
+
|
|
287
|
+
const handler = await httpHandler(error, {
|
|
288
|
+
cspNonce: "nonce-abc123", // Will be added to <style> tags
|
|
289
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### With Custom Error Logging
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import httpHandler from "@visulima/error-handler/handler/http/node";
|
|
297
|
+
|
|
298
|
+
const handler = await httpHandler(error, {
|
|
299
|
+
onError: (error, request, response) => {
|
|
300
|
+
// Log to your preferred logging service
|
|
301
|
+
console.error(`[${new Date().toISOString()}] ${request.method} ${request.url} - ${error.message}`);
|
|
302
|
+
|
|
303
|
+
// Or send to external logging service
|
|
304
|
+
// logToService({ error: error.message, url: request.url, method: request.method });
|
|
305
|
+
},
|
|
306
|
+
showTrace: process.env.NODE_ENV !== "production"
|
|
307
|
+
});
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## Additional Exports
|
|
311
|
+
|
|
312
|
+
This package provides additional exports for different use cases:
|
|
313
|
+
|
|
314
|
+
### Runtime-Specific Handlers
|
|
315
|
+
|
|
316
|
+
For convenience, you can also import runtime-specific handlers:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
// Deno
|
|
320
|
+
import fetchHandler from "@visulima/error-handler/handler/http/deno";
|
|
321
|
+
|
|
322
|
+
// Bun
|
|
323
|
+
import fetchHandler from "@visulima/error-handler/handler/http/bun";
|
|
324
|
+
|
|
325
|
+
// Cloudflare Workers
|
|
326
|
+
import fetchHandler from "@visulima/error-handler/handler/http/cloudflare";
|
|
327
|
+
|
|
328
|
+
// Edge Runtime
|
|
329
|
+
import fetchHandler from "@visulima/error-handler/handler/http/edge";
|
|
330
|
+
|
|
331
|
+
// Hono
|
|
332
|
+
import fetchHandler from "@visulima/error-handler/handler/http/hono";
|
|
333
|
+
|
|
334
|
+
// CLI applications
|
|
335
|
+
import cliHandler from "@visulima/error-handler/handler/cli";
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Individual Error Handlers
|
|
339
|
+
|
|
340
|
+
You can also import individual error handlers for specific content types:
|
|
341
|
+
|
|
342
|
+
```ts
|
|
343
|
+
import { htmlErrorHandler } from "@visulima/error-handler/error-handler/html";
|
|
344
|
+
import { problemErrorHandler } from "@visulima/error-handler/error-handler/problem";
|
|
345
|
+
import { jsonErrorHandler } from "@visulima/error-handler/error-handler/json";
|
|
346
|
+
import { jsonapiErrorHandler } from "@visulima/error-handler/error-handler/jsonapi";
|
|
347
|
+
import { textErrorHandler } from "@visulima/error-handler/error-handler/text";
|
|
348
|
+
import { jsonpErrorHandler } from "@visulima/error-handler/error-handler/jsonp";
|
|
349
|
+
import { xmlErrorHandler } from "@visulima/error-handler/error-handler/xml";
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### Main Export
|
|
353
|
+
|
|
354
|
+
```ts
|
|
355
|
+
import { createNegotiatedErrorHandler } from "@visulima/error-handler";
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Related
|
|
359
|
+
|
|
360
|
+
- [@visulima/flare](https://github.com/visulima/visulima/tree/main/packages/flare) - Full-featured error overlay with inspector
|
|
361
|
+
- [@visulima/error](https://github.com/visulima/visulima/tree/main/packages/error) - Error utilities and solution finders
|
|
362
|
+
|
|
363
|
+
## Supported Node.js Versions
|
|
364
|
+
|
|
365
|
+
Libraries in this ecosystem make the best effort to track [Node.js’ release schedule](https://github.com/nodejs/release#release-schedule).
|
|
366
|
+
Here’s [a post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
|
|
367
|
+
|
|
368
|
+
## Contributing
|
|
369
|
+
|
|
370
|
+
If you would like to help take a look at the [list of issues](https://github.com/visulima/visulima/issues) and check our [Contributing](.github/CONTRIBUTING.md) guidelines.
|
|
371
|
+
|
|
372
|
+
> **Note:** please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
|
|
373
|
+
|
|
374
|
+
## Credits
|
|
375
|
+
|
|
376
|
+
- [Daniel Bannert](https://github.com/prisis)
|
|
377
|
+
- [All Contributors](https://github.com/visulima/visulima/graphs/contributors)
|
|
378
|
+
|
|
379
|
+
## License
|
|
380
|
+
|
|
381
|
+
The visulima error-handler is open-sourced software licensed under the [MIT][license-url]
|
|
382
|
+
|
|
383
|
+
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
384
|
+
[typescript-url]: https://www.typescriptlang.org/ "TypeScript"
|
|
385
|
+
[license-image]: https://img.shields.io/npm/l/@visulima/error-handler?color=blueviolet&style=for-the-badge
|
|
386
|
+
[license-url]: LICENSE.md "license"
|
|
387
|
+
[npm-image]: https://img.shields.io/npm/v/@visulima/error-handler/latest.svg?style=for-the-badge&logo=npm
|
|
388
|
+
[npm-url]: https://www.npmjs.com/package/@visulima/error-handler/v/latest "npm"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { a as ErrorHandler } from '../packem_shared/types.d-DLy8CCca.js';
|
|
3
|
+
|
|
4
|
+
type HtmlErrorHandlerOptions = {
|
|
5
|
+
errorPage?: string | ((params: {
|
|
6
|
+
error: Error;
|
|
7
|
+
request: IncomingMessage;
|
|
8
|
+
response: ServerResponse;
|
|
9
|
+
reasonPhrase: string;
|
|
10
|
+
statusCode: number;
|
|
11
|
+
}) => string | Promise<string>);
|
|
12
|
+
cspNonce?: string;
|
|
13
|
+
};
|
|
14
|
+
declare const htmlErrorHandler: (options?: HtmlErrorHandlerOptions) => ErrorHandler;
|
|
15
|
+
|
|
16
|
+
export { type HtmlErrorHandlerOptions, htmlErrorHandler };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { getReasonPhrase } from 'http-status-codes';
|
|
2
|
+
import { a as addStatusCodeToResponse } from '../packem_shared/add-status-code-to-response-DdfxTQsK.js';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
const htmlErrorHandler = /* @__PURE__ */ __name((options = {}) => {
|
|
7
|
+
return async (error, request, response) => {
|
|
8
|
+
addStatusCodeToResponse(response, error);
|
|
9
|
+
response.setHeader("content-type", "text/html; charset=utf-8");
|
|
10
|
+
const title = getReasonPhrase(response.statusCode) || "Error";
|
|
11
|
+
const nonceAttr = options.cspNonce ? ` nonce="${options.cspNonce}"` : "";
|
|
12
|
+
if (options.errorPage) {
|
|
13
|
+
const override = typeof options.errorPage === "function" ? await options.errorPage({
|
|
14
|
+
error,
|
|
15
|
+
request,
|
|
16
|
+
response,
|
|
17
|
+
reasonPhrase: title,
|
|
18
|
+
statusCode: response.statusCode
|
|
19
|
+
}) : options.errorPage;
|
|
20
|
+
if (override) {
|
|
21
|
+
response.end(override);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
response.end(`<!DOCTYPE html>
|
|
26
|
+
<html lang="en">
|
|
27
|
+
<head>
|
|
28
|
+
<meta charset="utf-8">
|
|
29
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
30
|
+
|
|
31
|
+
<title>${title}</title>
|
|
32
|
+
<meta name="robots" content="noindex">
|
|
33
|
+
|
|
34
|
+
<style${nonceAttr}>
|
|
35
|
+
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}a{background-color:transparent}code{font-family:monospace,monospace;font-size:1em}[hidden]{display:none}html{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;line-height:1.5}*,:after,:before{box-sizing:border-box;border:0 solid #e2e8f0}a{color:inherit;text-decoration:inherit}code{font-family:Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace}svg,video{display:block;vertical-align:middle}video{max-width:100%;height:auto}.bg-white{--bg-opacity:1;background-color:#fff;background-color:rgba(255,255,255,var(--bg-opacity))}.bg-gray-100{--bg-opacity:1;background-color:#f7fafc;background-color:rgba(247,250,252,var(--bg-opacity))}.border-gray-200{--border-opacity:1;border-color:#edf2f7;border-color:rgba(237,242,247,var(--border-opacity))}.border-gray-400{--border-opacity:1;border-color:#cbd5e0;border-color:rgba(203,213,224,var(--border-opacity))}.border-r{border-right-width:1px}.flex{display:flex}.grid{display:grid}.hidden{display:none}.items-center{align-items:center}.justify-center{justify-content:center}.font-semibold{font-weight:600}.h-5{height:1.25rem}.h-8{height:2rem}.h-16{height:4rem}.text-sm{font-size:.875rem}.text-lg{font-size:1.125rem}.leading-7{line-height:1.75rem}.mx-auto{margin-left:auto;margin-right:auto}.ml-1{margin-left:.25rem}.mt-2{margin-top:.5rem}.mr-2{margin-right:.5rem}.ml-2{margin-left:.5rem}.mt-4{margin-top:1rem}.ml-4{margin-left:1rem}.mt-8{margin-top:2rem}.ml-12{margin-left:3rem}.-mt-px{margin-top:-1px}.max-w-xl{max-width:36rem}.max-w-6xl{max-width:72rem}.min-h-screen{min-height:100vh}.overflow-hidden{overflow:hidden}.p-6{padding:1.5rem}.py-4{padding-top:1rem;padding-bottom:1rem}.px-4{padding-left:1rem;padding-right:1rem}.px-6{padding-left:1.5rem;padding-right:1.5rem}.pt-8{padding-top:2rem}.fixed{position:fixed}.relative{position:relative}.top-0{top:0}.right-0{right:0}.shadow{box-shadow:0 1px 3px 0 rgba(0,0,0,.1),0 1px 2px 0 rgba(0,0,0,.06)}.text-center{text-align:center}.text-gray-200{--text-opacity:1;color:#edf2f7;color:rgba(237,242,247,var(--text-opacity))}.text-gray-300{--text-opacity:1;color:#e2e8f0;color:rgba(226,232,240,var(--text-opacity))}.text-gray-400{--text-opacity:1;color:#cbd5e0;color:rgba(203,213,224,var(--text-opacity))}.text-gray-500{--text-opacity:1;color:#a0aec0;color:rgba(160,174,192,var(--text-opacity))}.text-gray-600{--text-opacity:1;color:#718096;color:rgba(113,128,150,var(--text-opacity))}.text-gray-700{--text-opacity:1;color:#4a5568;color:rgba(74,85,104,var(--text-opacity))}.text-gray-900{--text-opacity:1;color:#1a202c;color:rgba(26,32,44,var(--text-opacity))}.uppercase{text-transform:uppercase}.underline{text-decoration:underline}.antialiased{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.tracking-wider{letter-spacing:.05em}.w-5{width:1.25rem}.w-8{width:2rem}.w-auto{width:auto}.grid-cols-1{grid-template-columns:repeat(1,minmax(0,1fr))}@media (min-width:640px){.sm\\:rounded-lg{border-radius:.5rem}.sm\\:block{display:block}.sm\\:items-center{align-items:center}.sm\\:justify-start{justify-content:flex-start}.sm\\:justify-between{justify-content:space-between}.sm\\:h-20{height:5rem}.sm\\:ml-0{margin-left:0}.sm\\:px-6{padding-left:1.5rem;padding-right:1.5rem}.sm\\:pt-0{padding-top:0}.sm\\:text-left{text-align:left}.sm\\:text-right{text-align:right}}@media (min-width:768px){.md\\:border-t-0{border-top-width:0}.md\\:border-l{border-left-width:1px}.md\\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}}@media (min-width:1024px){.lg\\:px-8{padding-left:2rem;padding-right:2rem}}@media (prefers-color-scheme:dark){.dark\\:bg-gray-800{--bg-opacity:1;background-color:#2d3748;background-color:rgba(45,55,72,var(--bg-opacity))}.dark\\:bg-gray-900{--bg-opacity:1;background-color:#1a202c;background-color:rgba(26,32,44,var(--bg-opacity))}.dark\\:border-gray-700{--border-opacity:1;border-color:#4a5568;border-color:rgba(74,85,104,var(--border-opacity))}.dark\\:text-white{--text-opacity:1;color:#fff;color:rgba(255,255,255,var(--text-opacity))}.dark\\:text-gray-400{--text-opacity:1;color:#cbd5e0;color:rgba(203,213,224,var(--text-opacity))}}
|
|
36
|
+
</style>
|
|
37
|
+
|
|
38
|
+
<style${nonceAttr}>
|
|
39
|
+
body {
|
|
40
|
+
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
|
|
41
|
+
}
|
|
42
|
+
</style>
|
|
43
|
+
</head>
|
|
44
|
+
<body class="antialiased">
|
|
45
|
+
<div class="relative flex items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-900">
|
|
46
|
+
<div class="max-w-xl mx-auto sm:px-6 lg:px-8">
|
|
47
|
+
<div class="flex items-center justify-center">
|
|
48
|
+
<div class="px-6 text-lg text-gray-600 dark:text-gray-400 border-r border-gray-400 dark:border-gray-700 tracking-wider font-semibold">
|
|
49
|
+
${response.statusCode}
|
|
50
|
+
</div>
|
|
51
|
+
|
|
52
|
+
<div class="ml-4 text-lg text-gray-600 dark:text-gray-400 uppercase tracking-wider">
|
|
53
|
+
${title}
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
</body>
|
|
59
|
+
</html>`);
|
|
60
|
+
};
|
|
61
|
+
}, "htmlErrorHandler");
|
|
62
|
+
|
|
63
|
+
export { htmlErrorHandler };
|