Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 7, 2024
1 parent 7345ca7 commit 0c75e01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
37 changes: 21 additions & 16 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function createError<DataT = unknown>(
return input;
}

// Inherit H3Error properties from cause as fallback
const cause: unknown = input.cause;

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

if (hasProp(input, "stack")) {
Expand All @@ -120,27 +123,29 @@ export function createError<DataT = unknown>(
const statusCode =
input.statusCode ??
input.status ??
(input?.cause as H3Error)?.statusCode ??
(input?.cause as { status?: number })?.status;

(cause as H3Error)?.statusCode ??
(cause as { status?: number })?.status;
if (typeof statusCode === "number") {
err.statusCode = sanitizeStatusCode(input.statusCode, err.statusCode);
err.statusCode = sanitizeStatusCode(statusCode);
}

if (input.statusMessage) {
err.statusMessage = input.statusMessage;
} else if (input.statusText) {
err.statusMessage = input.statusText as string;
}
if (err.statusMessage) {
err.statusMessage = sanitizeStatusMessage(err.statusMessage);
const statusMessage =
input.statusMessage ??
input.statusText ??
(cause as H3Error)?.statusMessage ??
(cause as { statusText?: string })?.statusText;
if (statusMessage) {
err.statusMessage = sanitizeStatusMessage(statusMessage);
}

if (input.fatal !== undefined) {
err.fatal = input.fatal;
const fatal = input.fatal ?? (cause as H3Error)?.fatal;
if (fatal !== undefined) {
err.fatal = fatal;

Check warning on line 143 in src/error.ts

View check run for this annotation

Codecov / codecov/patch

src/error.ts#L143

Added line #L143 was not covered by tests
}
if (input.unhandled !== undefined) {
err.unhandled = input.unhandled;

const unhandled = input.unhandled ?? (cause as H3Error)?.unhandled;
if (unhandled !== undefined) {
err.unhandled = unhandled;

Check warning on line 148 in src/error.ts

View check run for this annotation

Codecov / codecov/patch

src/error.ts#L148

Added line #L148 was not covered by tests
}

return err;
Expand Down
9 changes: 3 additions & 6 deletions test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,9 @@ describe("error", () => {
cause = new HttpError();
}

ctx.app.use(
"/",
eventHandler(() => {
throw createError(new CustomError());
}),
);
ctx.app.use("/", () => {
throw createError(new CustomError());
});

const res = await ctx.request.get("/");
expect(res.status).toBe(400);
Expand Down

0 comments on commit 0c75e01

Please sign in to comment.