Skip to content

Commit

Permalink
types: add generics to H3Error data and createError (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianGlowala authored Nov 20, 2023
1 parent ee17086 commit 7c48856
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import { hasProp } from "./utils/internal/object";
* @property {string} statusMessage - A string representing the HTTP status message.
* @property {boolean} fatal - Indicates if the error is a fatal error.
* @property {boolean} unhandled - Indicates if the error was unhandled and auto captured.
* @property {any} data - An extra data that will be included in the response.
* @property {DataT} data - An extra data that will be included in the response.
* This can be used to pass additional information about the error.
* @property {boolean} internal - Setting this property to `true` will mark the error as an internal error.
*/
export class H3Error extends Error {
export class H3Error<DataT = any> extends Error {
static __h3_error__ = true;
statusCode = 500;
fatal = false;
unhandled = false;
statusMessage?: string;
data?: any;
data?: DataT;
cause?: unknown;

constructor(message: string, opts: { cause?: unknown } = {}) {
Expand All @@ -40,7 +40,7 @@ export class H3Error extends Error {

toJSON() {
const obj: Pick<
H3Error,
H3Error<DataT>,
"message" | "statusCode" | "statusMessage" | "data"
> = {
message: this.message,
Expand All @@ -64,18 +64,20 @@ export class H3Error extends Error {
* @param input {string | (Partial<H3Error> & { status?: number; statusText?: string })} - The error message or an object containing error properties.
* @return {H3Error} - An instance of H3Error.
*/
export function createError(
input: string | (Partial<H3Error> & { status?: number; statusText?: string }),
export function createError<DataT = any>(
input:
| string
| (Partial<H3Error<DataT>> & { status?: number; statusText?: string }),
): H3Error {
if (typeof input === "string") {
return new H3Error(input);
return new H3Error<DataT>(input);
}

if (isError(input)) {
return input;
}

const err = new H3Error(input.message ?? input.statusMessage ?? "", {
const err = new H3Error<DataT>(input.message ?? input.statusMessage ?? "", {
cause: input.cause || input,
});

Expand Down

0 comments on commit 7c48856

Please sign in to comment.