Skip to content

Commit

Permalink
fix: catch format sql error
Browse files Browse the repository at this point in the history
  • Loading branch information
2976151305 committed Mar 20, 2024
1 parent aab67df commit 74bb7f4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions web/components/chat/chat-content/chart-view.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Datum } from '@antv/ava';
import { Table, Tabs, TabsProps } from 'antd';
import React from 'react';
import { format } from 'sql-formatter';
import { AutoChart, BackEndChartType, getChartType } from '@/components/chart/autoChart';
import { CodePreview } from './code-preview';
import { formatSql } from '@/utils';

function ChartView({ data, type, sql }: { data: Datum[]; type: BackEndChartType; sql: string }) {
const columns = data?.[0]
Expand All @@ -20,10 +20,11 @@ function ChartView({ data, type, sql }: { data: Datum[]; type: BackEndChartType;
label: 'Chart',
children: <AutoChart data={data} chartType={getChartType(type)} />,
};

const SqlItem = {
key: 'sql',
label: 'SQL',
children: <CodePreview language="sql" code={format(sql ?? '', { language: 'mysql' }) as string} />,
children: <CodePreview language="sql" code={formatSql(sql)} />,
};
const DataItem = {
key: 'data',
Expand Down
4 changes: 2 additions & 2 deletions web/components/chat/chat-content/config.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LinkOutlined, ReadOutlined, SyncOutlined } from '@ant-design/icons';
import ReactMarkdown from 'react-markdown';
import { Table, Image, Tag, Tabs, TabsProps, Popover } from 'antd';
import { format } from 'sql-formatter';
import { Reference } from '@/types/chat';
import { AutoChart, BackEndChartType, getChartType } from '@/components/chart';
import { CodePreview } from './code-preview';
Expand All @@ -15,6 +14,7 @@ import VisChart from './vis-chart';
import VisDashboard from './vis-dashboard';
import VisPlugin from './vis-plugin';
import VisCode from './vis-code';
import { formatSql } from '@/utils';

type MarkdownComponent = Parameters<typeof ReactMarkdown>['0']['components'];

Expand Down Expand Up @@ -228,7 +228,7 @@ const extraComponents: MarkdownComponent = {
const SqlItem = {
key: 'sql',
label: 'SQL',
children: <CodePreview code={format(data?.sql, { language: 'mysql' }) as string} language={'sql'} />,
children: <CodePreview code={formatSql(data?.sql, 'mysql')} language="sql" />,
};
const DataItem = {
key: 'data',
Expand Down
4 changes: 2 additions & 2 deletions web/components/chat/chat-content/vis-convert-error.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from 'sql-formatter';
import { CodePreview } from './code-preview';
import { formatSql } from '@/utils';

interface Props {
data: {
Expand All @@ -15,7 +15,7 @@ function VisConvertError({ data }: Props) {
<div className="p-3 text-white bg-red-500 whitespace-normal">{data.display_type}</div>
<div className="p-3 bg-red-50">
<div className="mb-2 whitespace-normal">{data.thought}</div>
<CodePreview code={format(data.sql)} language="sql" />
<CodePreview code={formatSql(data.sql)} language="sql" />
</div>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions web/components/chat/monaco-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
import Editor, { OnChange, loader } from '@monaco-editor/react';
import classNames from 'classnames';
import { useMemo } from 'react';
import { format } from 'sql-formatter';
import { formatSql } from '@/utils';

loader.config({ monaco });

Expand All @@ -21,9 +21,9 @@ export default function MonacoEditor({ className, value, language = 'mysql', onC
return value;
}
if (thoughts && thoughts.length > 0) {
return format(`-- ${thoughts} \n${value}`);
return formatSql(`-- ${thoughts} \n${value}`);
}
return format(value);
return formatSql(value);
}, [value, thoughts]);

return (
Expand Down
11 changes: 11 additions & 0 deletions web/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { format } from 'sql-formatter';

/** Theme */
export const STORAGE_THEME_KEY = '__db_gpt_theme_key';
/** Language */
Expand All @@ -7,5 +9,14 @@ export const STORAGE_INIT_MESSAGE_KET = '__db_gpt_im_key';
/** Flow nodes */
export const FLOW_NODES_KEY = '__db_gpt_static_flow_nodes_key';

export function formatSql(sql: string, lang?: string) {
if (!sql) return '';
try {
return format(sql, { language: lang });
} catch (e) {
return sql;
}
}

export * from './storage';
export * from './constants';

0 comments on commit 74bb7f4

Please sign in to comment.