h3 0.7.9 → 0.7.12

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/dist/index.cjs CHANGED
@@ -82,7 +82,7 @@ function handleCacheHeaders(event, opts) {
82
82
  const cacheControls = ["public"].concat(opts.cacheControls || []);
83
83
  let cacheMatched = false;
84
84
  if (opts.maxAge !== void 0) {
85
- opts.cacheControls?.push(`max-age=${+opts.maxAge}`, `s-maxage=${+opts.maxAge}`);
85
+ cacheControls.push(`max-age=${+opts.maxAge}`, `s-maxage=${+opts.maxAge}`);
86
86
  }
87
87
  if (opts.modifiedTime) {
88
88
  const modifiedTime = new Date(opts.modifiedTime);
@@ -183,14 +183,17 @@ class H3Error extends Error {
183
183
  constructor() {
184
184
  super(...arguments);
185
185
  this.statusCode = 500;
186
- this.statusMessage = "H3Error";
186
+ this.fatal = false;
187
+ this.unhandled = false;
188
+ this.statusMessage = "Internal Server Error";
187
189
  }
188
190
  }
191
+ H3Error.__h3_error__ = true;
189
192
  function createError(input) {
190
193
  if (typeof input === "string") {
191
194
  return new H3Error(input);
192
195
  }
193
- if (input instanceof H3Error) {
196
+ if (isError(input)) {
194
197
  return input;
195
198
  }
196
199
  const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : void 0);
@@ -203,6 +206,12 @@ function createError(input) {
203
206
  if (input.data) {
204
207
  err.data = input.data;
205
208
  }
209
+ if (input.fatal !== void 0) {
210
+ err.fatal = input.fatal;
211
+ }
212
+ if (input.unhandled !== void 0) {
213
+ err.unhandled = input.unhandled;
214
+ }
206
215
  return err;
207
216
  }
208
217
  function sendError(event, error, debug) {
@@ -228,7 +237,7 @@ function sendError(event, error, debug) {
228
237
  event.res.end(JSON.stringify(responseBody, null, 2));
229
238
  }
230
239
  function isError(input) {
231
- return input instanceof H3Error;
240
+ return input?.constructor?.__h3_error__ === true;
232
241
  }
233
242
 
234
243
  const defineHandler = (handler) => handler;
@@ -376,14 +385,18 @@ function createApp(options = {}) {
376
385
  const event = createEvent(req, res);
377
386
  try {
378
387
  await handler(event);
379
- } catch (err) {
388
+ } catch (_error) {
389
+ const error = createError(_error);
390
+ if (!isError(_error)) {
391
+ error.unhandled = true;
392
+ }
393
+ if (error.unhandled || error.fatal) {
394
+ console.error("[h3]", error.fatal ? "[fatal]" : "[unhandled]", error);
395
+ }
380
396
  if (options.onError) {
381
- await options.onError(err, event);
397
+ await options.onError(error, event);
382
398
  } else {
383
- if (!isError(err)) {
384
- console.error("[h3]", err);
385
- }
386
- await sendError(event, err, !!options.debug);
399
+ await sendError(event, error, !!options.debug);
387
400
  }
388
401
  }
389
402
  };
package/dist/index.d.ts CHANGED
@@ -98,12 +98,17 @@ declare function createAppEventHandler(stack: Stack, options: AppOptions): Event
98
98
  * @extends Error
99
99
  * @property {Number} statusCode An Integer indicating the HTTP response status code.
100
100
  * @property {String} statusMessage A String representing the HTTP status message
101
+ * @property {String} fatal Indicates if the error is a fatal error.
102
+ * @property {String} unhandled Indicates if the error was unhandled and auto captured.
101
103
  * @property {Any} data An extra data that will includes in the response.<br>
102
104
  * This can be used to pass additional information about the error.
103
105
  * @property {Boolean} internal Setting this property to <code>true</code> will mark error as an internal error
104
106
  */
105
107
  declare class H3Error extends Error {
108
+ static __h3_error__: boolean;
106
109
  statusCode: number;
110
+ fatal: boolean;
111
+ unhandled: boolean;
107
112
  statusMessage: string;
108
113
  data?: any;
109
114
  }
package/dist/index.mjs CHANGED
@@ -74,7 +74,7 @@ function handleCacheHeaders(event, opts) {
74
74
  const cacheControls = ["public"].concat(opts.cacheControls || []);
75
75
  let cacheMatched = false;
76
76
  if (opts.maxAge !== void 0) {
77
- opts.cacheControls?.push(`max-age=${+opts.maxAge}`, `s-maxage=${+opts.maxAge}`);
77
+ cacheControls.push(`max-age=${+opts.maxAge}`, `s-maxage=${+opts.maxAge}`);
78
78
  }
79
79
  if (opts.modifiedTime) {
80
80
  const modifiedTime = new Date(opts.modifiedTime);
@@ -175,14 +175,17 @@ class H3Error extends Error {
175
175
  constructor() {
176
176
  super(...arguments);
177
177
  this.statusCode = 500;
178
- this.statusMessage = "H3Error";
178
+ this.fatal = false;
179
+ this.unhandled = false;
180
+ this.statusMessage = "Internal Server Error";
179
181
  }
180
182
  }
183
+ H3Error.__h3_error__ = true;
181
184
  function createError(input) {
182
185
  if (typeof input === "string") {
183
186
  return new H3Error(input);
184
187
  }
185
- if (input instanceof H3Error) {
188
+ if (isError(input)) {
186
189
  return input;
187
190
  }
188
191
  const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : void 0);
@@ -195,6 +198,12 @@ function createError(input) {
195
198
  if (input.data) {
196
199
  err.data = input.data;
197
200
  }
201
+ if (input.fatal !== void 0) {
202
+ err.fatal = input.fatal;
203
+ }
204
+ if (input.unhandled !== void 0) {
205
+ err.unhandled = input.unhandled;
206
+ }
198
207
  return err;
199
208
  }
200
209
  function sendError(event, error, debug) {
@@ -220,7 +229,7 @@ function sendError(event, error, debug) {
220
229
  event.res.end(JSON.stringify(responseBody, null, 2));
221
230
  }
222
231
  function isError(input) {
223
- return input instanceof H3Error;
232
+ return input?.constructor?.__h3_error__ === true;
224
233
  }
225
234
 
226
235
  const defineHandler = (handler) => handler;
@@ -368,14 +377,18 @@ function createApp(options = {}) {
368
377
  const event = createEvent(req, res);
369
378
  try {
370
379
  await handler(event);
371
- } catch (err) {
380
+ } catch (_error) {
381
+ const error = createError(_error);
382
+ if (!isError(_error)) {
383
+ error.unhandled = true;
384
+ }
385
+ if (error.unhandled || error.fatal) {
386
+ console.error("[h3]", error.fatal ? "[fatal]" : "[unhandled]", error);
387
+ }
372
388
  if (options.onError) {
373
- await options.onError(err, event);
389
+ await options.onError(error, event);
374
390
  } else {
375
- if (!isError(err)) {
376
- console.error("[h3]", err);
377
- }
378
- await sendError(event, err, !!options.debug);
391
+ await sendError(event, error, !!options.debug);
379
392
  }
380
393
  }
381
394
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "h3",
3
- "version": "0.7.9",
3
+ "version": "0.7.12",
4
4
  "description": "Tiny JavaScript Server",
5
5
  "repository": "unjs/h3",
6
6
  "license": "MIT",
@@ -22,7 +22,7 @@
22
22
  "cookie-es": "^0.5.0",
23
23
  "destr": "^1.1.1",
24
24
  "radix3": "^0.1.2",
25
- "ufo": "^0.8.4"
25
+ "ufo": "^0.8.5"
26
26
  },
27
27
  "devDependencies": {
28
28
  "0x": "latest",
@@ -45,7 +45,7 @@
45
45
  "unbuild": "latest",
46
46
  "vitest": "latest"
47
47
  },
48
- "packageManager": "pnpm@7.1.9",
48
+ "packageManager": "pnpm@7.5.2",
49
49
  "scripts": {
50
50
  "build": "unbuild",
51
51
  "dev": "vitest",