Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(createError): inherit fields from cause as fallback #795

Merged
merged 5 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@
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 @@ -117,25 +120,32 @@
err.data = input.data;
}

if (input.statusCode) {
err.statusCode = sanitizeStatusCode(input.statusCode, err.statusCode);
} else if (input.status) {
err.statusCode = sanitizeStatusCode(input.status, err.statusCode);
}
if (input.statusMessage) {
err.statusMessage = input.statusMessage;
} else if (input.statusText) {
err.statusMessage = input.statusText as string;
const statusCode =
input.statusCode ??
input.status ??
(cause as H3Error)?.statusCode ??
(cause as { status?: number })?.status;
if (typeof statusCode === "number") {
err.statusCode = sanitizeStatusCode(statusCode);
}
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
17 changes: 17 additions & 0 deletions test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,21 @@ describe("error", () => {

expect(ctx.errors[0].cause).toBeInstanceOf(CustomError);
});

it("can parse statusCode from cause", async () => {
class HttpError extends Error {
statusCode = 400;
}

class CustomError extends Error {
cause = new HttpError();
}

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

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