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

Replacing originalError with cause on validation errors #2137

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/common-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const getMessageFromError = (error: Error): string => {
.join("; ");
}
if (error instanceof OutputValidationError) {
const hasFirstField = error.originalError.issues[0]?.path.length > 0;
const hasFirstField = error.cause.issues[0]?.path.length > 0;
return `output${hasFirstField ? "/" : ": "}${error.message}`;
}
return error.message;
Expand Down
28 changes: 22 additions & 6 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,33 @@ export class IOSchemaError extends Error {
export class OutputValidationError extends IOSchemaError {
public override name = "OutputValidationError";

constructor(public readonly originalError: z.ZodError) {
super(getMessageFromError(originalError));
constructor(public override readonly cause: z.ZodError) {
super(getMessageFromError(cause), { cause });
}

/**
* @deprecated use the cause property instead
* @todo remove in v21
* */
public get originalError() {
return this.cause;
}
}

/** @desc An error of validating the input sources against the Middleware or Endpoint input schema */
export class InputValidationError extends IOSchemaError {
public override name = "InputValidationError";

constructor(public readonly originalError: z.ZodError) {
super(getMessageFromError(originalError));
constructor(public override readonly cause: z.ZodError) {
super(getMessageFromError(cause), { cause });
}

/**
* @deprecated use the cause property instead
* @todo remove in v21
* */
public get originalError() {
return this.cause;
}
}

Expand All @@ -58,9 +74,9 @@ export class ResultHandlerError extends Error {

constructor(
message: string,
public readonly originalError?: Error,
public override readonly cause?: Error,
) {
super(message);
super(message, { cause });
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/last-resort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export const lastResortHandler = ({
.type("text/plain")
.end(
`An error occurred while serving the result: ${error.message}.` +
(error.originalError
? `\nOriginal error: ${error.originalError.message}.`
: ""),
(error.cause ? `\nOriginal error: ${error.cause.message}.` : ""),
);
};
8 changes: 5 additions & 3 deletions tests/unit/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ describe("Errors", () => {
expect(error.name).toBe("OutputValidationError");
});

test("should have .originalError property matching the one used for constructing", () => {
test("should have .cause property matching the one used for constructing", () => {
expect(error.cause).toEqual(zodError);
expect(error.originalError).toEqual(zodError);
});
});
Expand All @@ -86,6 +87,7 @@ describe("Errors", () => {
});

test("should have .originalError property matching the one used for constructing", () => {
RobinTail marked this conversation as resolved.
Show resolved Hide resolved
expect(error.cause).toEqual(zodError);
expect(error.originalError).toEqual(zodError);
});
});
Expand All @@ -103,8 +105,8 @@ describe("Errors", () => {
expect(error.name).toBe("ResultHandlerError");
});

test(".originalError should be the original error", () => {
expect(error.originalError).toEqual(originalError);
test(".cause should be the original error", () => {
expect(error.cause).toEqual(originalError);
});
},
);
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/last-resort.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ResultHandlerError } from "../../src/errors";
import { lastResortHandler } from "../../src/last-resort";
import { makeLoggerMock, makeResponseMock } from "../../src/testing";

Expand All @@ -12,7 +13,10 @@ describe("Last Resort Handler", () => {
lastResortHandler({
logger: loggerMock,
response: responseMock,
error: new Error("something went wrong"),
error: new ResultHandlerError(
"something went wrong",
new Error("what exactly"),
),
});
expect(loggerMock._getLogs().error).toEqual([
["Result handler failure: something went wrong."],
Expand All @@ -23,7 +27,7 @@ describe("Last Resort Handler", () => {
"text/plain",
);
expect(responseMock._getData()).toBe(
"An error occurred while serving the result: something went wrong.",
"An error occurred while serving the result: something went wrong.\nOriginal error: what exactly.",
);
});
});