Skip to content

Commit 7887369

Browse files
authored
fix: fallback if unipika convert json failed (#2227)
1 parent 0ec2f98 commit 7887369

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/components/JsonViewer/JsonViewer.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ import ArrowUpFromLineIcon from '@gravity-ui/icons/svgs/arrow-up-from-line.svg';
2727

2828
import './JsonViewer.scss';
2929

30-
interface JsonViewerProps {
31-
value: UnipikaValue;
30+
interface JsonViewerCommonProps {
3231
unipikaSettings?: UnipikaSettings;
3332
extraTools?: React.ReactNode;
3433
tableSettings?: DT100.Settings;
3534
search?: boolean;
3635
collapsedInitially?: boolean;
3736
}
3837

38+
interface JsonViewerProps extends JsonViewerCommonProps {
39+
value: UnipikaValue | {_error: string};
40+
}
41+
42+
interface JsonViewerComponentProps extends JsonViewerCommonProps {
43+
value: UnipikaValue;
44+
}
45+
3946
interface State {
4047
flattenResult: FlattenUnipikaResult;
4148
value: JsonViewerProps['value'];
@@ -88,14 +95,26 @@ function calculateState(
8895
);
8996
}
9097

91-
export function JsonViewer({
98+
function isUnipikaValue(value: UnipikaValue | {_error: string}): value is UnipikaValue {
99+
return !('_error' in value);
100+
}
101+
102+
export function JsonViewer(props: JsonViewerProps) {
103+
const {value} = props;
104+
if (!isUnipikaValue(value)) {
105+
return value._error;
106+
}
107+
return <JsonViewerComponent {...props} value={value} />;
108+
}
109+
110+
function JsonViewerComponent({
92111
tableSettings,
93112
value,
94113
unipikaSettings,
95114
search = true,
96115
extraTools,
97116
collapsedInitially,
98-
}: JsonViewerProps) {
117+
}: JsonViewerComponentProps) {
99118
const [caseSensitiveSearch, setCaseSensitiveSearch] = useSetting(
100119
CASE_SENSITIVE_JSON_SEARCH,
101120
false,

src/components/JsonViewer/unipika/unipika.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ export const defaultUnipikaSettings = {
1515
};
1616

1717
export function unipikaConvert(value: unknown) {
18-
return unipika.converters.yson(value, defaultUnipikaSettings);
18+
let result;
19+
try {
20+
result = unipika.converters.yson(value, defaultUnipikaSettings);
21+
} catch (e) {
22+
console.error(e);
23+
result = {_error: 'JSON is invalid'};
24+
}
25+
return result;
1926
}
2027

2128
export function useUnipikaConvert(value: unknown) {

0 commit comments

Comments
 (0)