Skip to content

Commit 8c94f9c

Browse files
authored
fix(sqllab): Invalid SQL Error breaks SQL Lab (#33164)
1 parent b589d44 commit 8c94f9c

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

superset-frontend/src/components/ErrorMessage/InvalidSQLErrorMessage.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ const defaultProps = {
3636
subtitle: 'Test subtitle',
3737
};
3838

39+
const missingExtraProps = {
40+
...defaultProps,
41+
error: {
42+
error_type: ErrorTypeEnum.INVALID_SQL_ERROR,
43+
message: 'SQLStatement should have exactly one statement',
44+
level: 'error' as ErrorLevel,
45+
extra: {
46+
sql: null,
47+
line: null,
48+
column: null,
49+
engine: null,
50+
},
51+
},
52+
};
53+
3954
const renderComponent = (overrides = {}) =>
4055
render(<InvalidSQLErrorMessage {...defaultProps} {...overrides} />);
4156

@@ -60,6 +75,12 @@ describe('InvalidSQLErrorMessage', () => {
6075
unmount();
6176
});
6277

78+
it('renders the error message with the empty extra properties', () => {
79+
const { getByText } = renderComponent(missingExtraProps);
80+
expect(getByText('Unable to parse SQL')).toBeInTheDocument();
81+
expect(getByText(missingExtraProps.error.message)).toBeInTheDocument();
82+
});
83+
6384
it('displays the SQL error line and column indicator', async () => {
6485
const { getByText, container, unmount } = renderComponent();
6586

superset-frontend/src/components/ErrorMessage/InvalidSQLErrorMessage.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ function InvalidSQLErrorMessage({
3636
source,
3737
subtitle,
3838
}: ErrorMessageComponentProps<SupersetParseErrorExtra>) {
39-
const { extra, level } = error;
39+
const { extra, level, message } = error;
4040

4141
const { sql, line, column } = extra;
42-
const lines = sql.split('\n');
42+
const lines = sql?.split('\n');
4343
let errorLine;
44-
if (line !== null) errorLine = lines[line - 1];
45-
else if (lines.length > 0) {
44+
if (line !== null && Number.isInteger(line)) errorLine = lines[line - 1];
45+
else if (lines?.length > 0) {
4646
errorLine = lines[0];
4747
}
48-
const body = errorLine && (
48+
const body = errorLine ? (
4949
<>
5050
<pre>{errorLine}</pre>
5151
{column !== null && <pre>{' '.repeat(column - 1)}^</pre>}
5252
</>
53+
) : (
54+
message
5355
);
5456
return (
5557
<ErrorAlert

superset/sql_parse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ def _extract_tables_from_sql(self) -> set[Table]:
277277
"You may have an error in your SQL statement. {message}"
278278
).format(message=ex.error.message),
279279
level=ErrorLevel.ERROR,
280+
extra=ex.error.extra,
280281
)
281282
) from ex
282283

0 commit comments

Comments
 (0)