diff --git a/packages/kit/src/exports/internal/index.js b/packages/kit/src/exports/internal/index.js index b87448b30914..e297beb30cd4 100644 --- a/packages/kit/src/exports/internal/index.js +++ b/packages/kit/src/exports/internal/index.js @@ -1,17 +1,20 @@ -export class HttpError { +export class HttpError extends Error { /** * @param {number} status * @param {{message: string} extends App.Error ? (App.Error | string | undefined) : App.Error} body */ constructor(status, body) { + const normalized = + typeof body === 'string' ? { message: body } : body ?? { message: `Error: ${status}` }; + + const message = + typeof normalized?.message === 'string' ? normalized.message : `Error: ${status}`; + + super(message); + + this.name = 'HttpError'; this.status = status; - if (typeof body === 'string') { - this.body = { message: body }; - } else if (body) { - this.body = body; - } else { - this.body = { message: `Error: ${status}` }; - } + this.body = normalized; } toString() { diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 0ddf7ca08844..e6ecb6b604f6 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -2706,7 +2706,7 @@ declare module '@sveltejs/kit' { export type LessThan = TNumber extends TArray["length"] ? TArray[number] : LessThan; export type NumericRange = Exclude, LessThan>; export const VERSION: string; - class HttpError_1 { + class HttpError_1 extends Error { constructor(status: number, body: { message: string;