Skip to content

Commit 86e80a6

Browse files
committed
feat(prisma-parser): transform parsing error location
1 parent ecfae3e commit 86e80a6

File tree

1 file changed

+46
-3
lines changed
  • packages/prisma-to-json-table-schema/src

1 file changed

+46
-3
lines changed
Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
1-
import { getSchema } from "@mrleebo/prisma-ast";
1+
import { type CstNodeLocation, getSchema } from "@mrleebo/prisma-ast";
22
import { type JSONTableSchema } from "shared/types/tableSchema";
3+
import { DiagnosticError } from "shared/types/diagnostic";
34

45
import { prismaASTToJSONTableSchema } from "./utils/transformers/prismaASTToJSONTableSchema";
56

67
export const parsePrismaToJSON = (prismaCode: string): JSONTableSchema => {
7-
const rawParsedSchema = getSchema(prismaCode);
8-
return prismaASTToJSONTableSchema(rawParsedSchema);
8+
try {
9+
const rawParsedSchema = getSchema(prismaCode);
10+
return prismaASTToJSONTableSchema(rawParsedSchema);
11+
} catch (error) {
12+
if ("token" in (error as any)) {
13+
const token = (error as any).token as CstNodeLocation;
14+
15+
const endColumn = token.endColumn;
16+
const endLine = token.endLine;
17+
const startColumn = token.startColumn;
18+
const startLine = token.startLine;
19+
20+
if (
21+
endColumn === undefined ||
22+
startColumn === undefined ||
23+
endLine === undefined ||
24+
startLine === undefined
25+
) {
26+
throw error;
27+
}
28+
29+
const locationEnd = { column: endColumn, line: endLine };
30+
const locationStart = {
31+
column: startColumn,
32+
line: startLine,
33+
};
34+
35+
throw new DiagnosticError(
36+
{
37+
end: {
38+
column: locationEnd.column - 1,
39+
line: locationEnd.line - 1,
40+
},
41+
start: {
42+
column: locationStart.column - 1,
43+
line: locationStart.line - 1,
44+
},
45+
},
46+
String((error as Error).message),
47+
);
48+
}
49+
50+
throw error;
51+
}
952
};

0 commit comments

Comments
 (0)