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

Drop originalError property on validation errors #2139

Merged
merged 4 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Properties `httpServer` and `httpsServer` are removed;
- Added `servers` property — array containing those server instances in the same order.
- The `serializer` property of `Documentation` and `Integration` constructor argument removed;
- The `originalError` property of `InputValidationError` and `OutputValidationError` removed (use `cause` instead);
- Consider the automated migration using the built-in ESLint rule.

```js
Expand Down
16 changes: 0 additions & 16 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ export class OutputValidationError extends IOSchemaError {
constructor(public override readonly cause: z.ZodError) {
super(getMessageFromError(cause), { cause });
}

/**
* @deprecated use the cause property instead
* @todo remove in v21
* */
public get originalError() {
RobinTail marked this conversation as resolved.
Show resolved Hide resolved
return this.cause;
}
}

/** @desc An error of validating the input sources against the Middleware or Endpoint input schema */
Expand All @@ -58,14 +50,6 @@ export class InputValidationError extends IOSchemaError {
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 related to the execution or incorrect configuration of ResultHandler */
Expand Down
23 changes: 22 additions & 1 deletion src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const createServerName = "createServer";
const serverPropName = "server";
const httpServerPropName = "httpServer";
const httpsServerPropName = "httpsServer";
const originalErrorPropName = "originalError";

const changedProps = {
[serverPropName]: "http",
[httpServerPropName]: "servers",
[httpsServerPropName]: "servers",
[originalErrorPropName]: "cause",
};

const movedProps = [
Expand Down Expand Up @@ -57,7 +59,26 @@ const v21 = ESLintUtils.RuleCreator.withoutDocs({
},
defaultOptions: [],
create: (ctx) => ({
CallExpression: (node) => {
[NT.MemberExpression]: (node) => {
if (
node.property.type === NT.Identifier &&
node.property.name === originalErrorPropName &&
node.object.type === NT.Identifier &&
node.object.name.match(/err/i) // this is probably an error instance, but we don't do type checking
) {
const replacement = changedProps[node.property.name];
ctx.report({
node: node.property,
messageId: "change",
data: {
subject: "property",
from: node.property.name,
to: replacement,
},
});
}
},
[NT.CallExpression]: (node) => {
if (node.callee.type !== NT.Identifier) return;
if (
node.callee.name === createConfigName &&
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe("Errors", () => {

test("should have .cause property matching the one used for constructing", () => {
expect(error.cause).toEqual(zodError);
expect(error.originalError).toEqual(zodError);
});
});

Expand All @@ -88,7 +87,6 @@ describe("Errors", () => {

test("should have .cause property matching the one used for constructing", () => {
expect(error.cause).toEqual(zodError);
expect(error.originalError).toEqual(zodError);
});
});

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tester.run("v21", migration.rules.v21, {
`createConfig({ http: {} });`,
`createConfig({ http: { listen: 8090 }, upload: true });`,
`const { app, servers, logger } = await createServer();`,
`const error = new Error(); console.error(error.cause?.message);`,
],
invalid: [
{
Expand Down Expand Up @@ -63,5 +64,14 @@ tester.run("v21", migration.rules.v21, {
},
],
},
{
code: `const error = new Error(); console.error(error.originalError?.message);`,
errors: [
{
messageId: "change",
data: { subject: "property", from: "originalError", to: "cause" },
},
],
},
],
});