Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
import { type IRange, Selection } from "monaco-editor";
import { type ReactNode, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { positionWithOffset } from "@/components/monaco/utils";
import { Alert } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { activeSQLEditorRef } from "@/modules/sql-editor/components/StandardPanel/state";
import { sqlEditorEvents } from "@/modules/sql-editor/model/events";
import type { SQLEditorQueryParams, SQLResultSetV1 } from "@/types";
import { PostgresError } from "./PostgresError";
import { statementAsTyped } from "./statementText";
interface ErrorViewProps {
error: string | undefined;
executeParams?: SQLEditorQueryParams;
resultSet?: SQLResultSetV1;
suffix?: ReactNode;
}
export function ErrorView({
error,
executeParams,
resultSet,
suffix,
}: ErrorViewProps) {
const { t } = useTranslation();
const failingStatement = useMemo(() => {
const failed = resultSet?.results?.find((r) => r.error);
return failed?.statement?.trim();
}, [resultSet]);
const errorPosition = useMemo(() => {
const result = resultSet?.results?.[0];
if (result?.detailedError.case === "syntaxError") {
return result.detailedError.value.startPosition;
}
return undefined;
}, [resultSet]);
const hasErrorPosition = (errorPosition?.line ?? 0) > 0;
const canShowInEditor = hasErrorPosition || !!failingStatement;
const findStatementRange = (): IRange | undefined => {
if (!failingStatement) return undefined;
const editor = activeSQLEditorRef.value;
const model = editor?.getModel();
if (!model) return undefined;
const statement = statementAsTyped(failingStatement);
if (!statement) return undefined;
let matches = model.findMatches(
statement,
false,
false,
false,
null,
false
);
if (matches.length > 0) return matches[0].range;
matches = model.findMatches(
`${statement};`,
false,
false,
false,
null,
false
);
if (matches.length > 0) return matches[0].range;
return undefined;
};
const positionLabel = (() => {
if (hasErrorPosition && errorPosition) {
const [line, col] = positionWithOffset(
errorPosition.line,
errorPosition.column,
executeParams?.selection
);
return `L${line}:C${col}`;
}
return t("sql-editor.show-in-editor");
})();
const showInEditor = () => {
let range: IRange | undefined;
if (hasErrorPosition && errorPosition) {
const [line, col] = positionWithOffset(
errorPosition.line,
errorPosition.column,
executeParams?.selection
);
range = new Selection(line, col, line, col);
} else {
range = findStatementRange();
}
if (range) {
sqlEditorEvents.emit("set-editor-selection", range);
}
};
return (
<div
className={cn(
"w-full text-md font-normal flex flex-col gap-2 text-sm",
"text-control-light"
)}
>
<Alert variant="error" className="w-full">
<div className="flex items-center gap-2 w-full">
<span className="flex-1">{error}</span>
{canShowInEditor && (
<Button size="sm" appearance="secondary" onClick={showInEditor}>
{positionLabel}
</Button>
)}
</div>
</Alert>
{suffix && <div>{suffix}</div>}
{resultSet && <PostgresError resultSet={resultSet} />}
</div>
);
}