Skip to content

Commit

Permalink
feat: fail gracefully on fatal error (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbie012 authored Jan 8, 2025
1 parent aae9fb5 commit b5f7c18
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/short-wombats-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ts-safeql/eslint-plugin": patch
---

Fail SafeQL gracefully on fatal error
15 changes: 15 additions & 0 deletions packages/eslint-plugin/src/rules/check-sql.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { zodToJsonSchema } from "zod-to-json-schema";
import { ESTreeUtils } from "../utils";
import { E, J, flow, pipe } from "../utils/fp-ts";
import { getResolvedTargetByTypeNode } from "../utils/get-resolved-target-by-type-node";
import { isInEditorEnv } from "../utils/is-in-editor";
import { memoize } from "../utils/memoize";
import { locateNearestPackageJsonDir } from "../utils/node.utils";
import { mapTemplateLiteralToQueryText } from "../utils/ts-pg.utils";
Expand Down Expand Up @@ -143,6 +144,8 @@ const generateSyncE = flow(
E.mapLeft((error) => error as unknown as WorkerError),
);

let fatalError: WorkerError | undefined;

function reportCheck(params: {
context: RuleContext;
tag: TSESTree.TaggedTemplateExpression;
Expand All @@ -154,6 +157,14 @@ function reportCheck(params: {
}) {
const { context, tag, connection, target, projectDir, typeParameter, baseNode } = params;

if (fatalError !== undefined) {
const hint = isInEditorEnv()
? "If you think this is a bug, please open an issue. If not, please try to fix the error and restart ESLint."
: "If you think this is a bug, please open an issue.";

return reportBaseError({ context, error: fatalError, tag, hint });
}

const nullAsOptional = connection.nullAsOptional ?? false;
const nullAsUndefined = connection.nullAsUndefined ?? false;

Expand Down Expand Up @@ -203,6 +214,10 @@ function reportCheck(params: {
{ _tag: "DatabaseInitializationError" },
{ _tag: "InternalError" },
(error) => {
if (params.connection.keepAlive === true) {
fatalError = error;
}

return reportBaseError({ context, error, tag });
},
)
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/check-sql.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ export function reportBaseError(params: {
context: RuleContext;
tag: TSESTree.TaggedTemplateExpression;
error: WorkerError;
hint?: string;
}) {
const { context, tag, error } = params;

return context.report({
node: tag,
messageId: "error",
data: {
error: error.message,
error: [error.message, fmap(params.hint, (hint) => `Hint: ${hint}`)]
.filter(Boolean)
.join("\n"),
},
});
}
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/src/utils/is-in-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// https://github.com/antfu/eslint-config/blob/de2d48d0f4409c66fccbf0872896f00c202f7bf1/src/utils.ts#L135
export function isInEditorEnv(): boolean {
if (process.env.CI) return false;
if (isInGitHooksOrLintStaged()) return false;
return !!(
false ||
process.env.VSCODE_PID ||
process.env.VSCODE_CWD ||
process.env.JETBRAINS_IDE ||
process.env.VIM ||
process.env.NVIM
);
}

export function isInGitHooksOrLintStaged(): boolean {
return !!(
false ||
process.env.GIT_PARAMS ||
process.env.VSCODE_GIT_COMMAND ||
process.env.npm_lifecycle_script?.startsWith("lint-staged")
);
}

0 comments on commit b5f7c18

Please sign in to comment.