Display sql error location and message on import

This commit is contained in:
1ilit 2024-04-20 18:16:41 +03:00
parent e8ea47fd3f
commit 898d81d6e9
2 changed files with 43 additions and 6 deletions

View File

@ -1,7 +1,12 @@
import { Upload, Checkbox } from "@douyinfe/semi-ui"; import { Upload, Checkbox, Banner } from "@douyinfe/semi-ui";
import { STATUS } from "../../../data/constants"; import { STATUS } from "../../../data/constants";
export default function ImportSource({ importData, setImportData, setError }) { export default function ImportSource({
importData,
setImportData,
error,
setError,
}) {
return ( return (
<div> <div>
<Upload <Upload
@ -60,6 +65,29 @@ export default function ImportSource({ importData, setImportData, setError }) {
> >
Overwrite existing diagram Overwrite existing diagram
</Checkbox> </Checkbox>
<div className="mt-2">
{error.type === STATUS.ERROR ? (
<Banner
type="danger"
fullMode={false}
description={<div>{error.message}</div>}
/>
) : error.type === STATUS.OK ? (
<Banner
type="info"
fullMode={false}
description={<div>{error.message}</div>}
/>
) : (
error.type === STATUS.WARNING && (
<Banner
type="warning"
fullMode={false}
description={<div>{error.message}</div>}
/>
)
)}
</div>
</div> </div>
</div> </div>
); );

View File

@ -114,9 +114,17 @@ export default function Modal({
try { try {
ast = parser.astify(importSource.src, { database: "MySQL" }); ast = parser.astify(importSource.src, { database: "MySQL" });
} catch (err) { } catch (err) {
Toast.error( setError({
"Could not parse the sql file. Make sure there are no syntax errors.", type: STATUS.ERROR,
); message:
err.name +
" [Ln " +
err.location.start.line +
", Col " +
err.location.start.column +
"]: " +
err.message,
});
return; return;
} }
@ -133,6 +141,7 @@ export default function Modal({
setTables((prev) => [...prev, ...d.tables]); setTables((prev) => [...prev, ...d.tables]);
setRelationships((prev) => [...prev, ...d.relationships]); setRelationships((prev) => [...prev, ...d.relationships]);
} }
setModal(MODAL.NONE);
}; };
const createNewDiagram = (id) => { const createNewDiagram = (id) => {
@ -167,7 +176,6 @@ export default function Modal({
return; return;
case MODAL.IMPORT_SRC: case MODAL.IMPORT_SRC:
parseSQLAndLoadDiagram(); parseSQLAndLoadDiagram();
setModal(MODAL.NONE);
return; return;
case MODAL.OPEN: case MODAL.OPEN:
if (selectedDiagramId === 0) return; if (selectedDiagramId === 0) return;
@ -207,6 +215,7 @@ export default function Modal({
<ImportSource <ImportSource
importData={importSource} importData={importSource}
setImportData={setImportSource} setImportData={setImportSource}
error={error}
setError={setError} setError={setError}
/> />
); );