Skip to content

Commit

Permalink
try catch on whole query
Browse files Browse the repository at this point in the history
  • Loading branch information
jczhong84 committed Feb 8, 2023
1 parent b1533e1 commit 7c712d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
10 changes: 5 additions & 5 deletions querybook/webapp/components/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ export const QueryEditor: React.FC<
);

const formatQuery = useCallback(
(
options: ISQLFormatOptions = {
silent: false,
}
) => {
(options: ISQLFormatOptions) => {
options = {
silent: false, // default false to throw format errors
...options,
};
if (editorRef.current) {
const indentWithTabs =
editorRef.current.getOption('indentWithTabs');
Expand Down
63 changes: 27 additions & 36 deletions querybook/webapp/lib/sql-helper/sql-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,31 +153,19 @@ function formatEachStatement(
language: string,
options: ISQLFormatOptions
) {
const safeSQLFormat = (text: string) => {
try {
return sqlFormat(text, {
tabWidth: options.tabWidth,
language: getLanguageForSqlFormatter(language),
useTabs: options.useTabs,
});
} catch (e) {
if (options.silent) {
return text;
} else {
throw e;
}
}
};

const formattedStatements: string[] = statements.map(
return statements.map(
({ firstKeyWord, statementText, idToTemplateTag }) => {
// Use standard formatter to format
let formattedStatement = statementText;
if (
firstKeyWord &&
allowedStatement.has(firstKeyWord.text.toLocaleLowerCase())
) {
formattedStatement = safeSQLFormat(statementText);
formattedStatement = sqlFormat(statementText, {
tabWidth: options.tabWidth,
language: getLanguageForSqlFormatter(language),
useTabs: options.useTabs,
});
}

for (const [id, templateTag] of Object.entries(idToTemplateTag)) {
Expand All @@ -190,8 +178,6 @@ function formatEachStatement(
return formattedStatement;
}
);

return formattedStatements;
}

export function format(
Expand All @@ -210,22 +196,27 @@ export function format(
...options,
};

const { processedStatements, newLineBetweenStatement } = processStatements(
query,
language,
options
);
const formattedStatements = formatEachStatement(
processedStatements,
language,
options
);

return formattedStatements.reduce(
(acc, statement, index) =>
acc + '\n'.repeat(newLineBetweenStatement[index]) + statement,
''
);
try {
const { processedStatements, newLineBetweenStatement } =
processStatements(query, language, options);
const formattedStatements = formatEachStatement(
processedStatements,
language,
options
);

return formattedStatements.reduce(
(acc, statement, index) =>
acc + '\n'.repeat(newLineBetweenStatement[index]) + statement,
''
);
} catch (e) {
if (options.silent) {
return query;
} else {
throw e;
}
}
}

// Override according to https://github.com/sql-formatter-org/sql-formatter/blob/master/docs/language.md
Expand Down

0 comments on commit 7c712d9

Please sign in to comment.