Skip to content

Commit 1536824

Browse files
#RI-5856 - handle yaml error
1 parent 32f007d commit 1536824

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

redisinsight/ui/src/pages/rdi/pipeline-management/components/jobs-panel/Panel.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
} from '@elastic/eui'
1414
import { useDispatch, useSelector } from 'react-redux'
1515
import { useParams } from 'react-router-dom'
16-
import yaml from 'js-yaml'
1716

1817
import { PipelineJobsTabs } from 'uiSrc/slices/interfaces/rdi'
1918
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
@@ -26,7 +25,7 @@ import { formatLongName } from 'uiSrc/utils'
2625
import styles from './styles.module.scss'
2726

2827
export interface Props {
29-
job: string
28+
job?: object
3029
onClose: () => void
3130
}
3231

@@ -108,7 +107,7 @@ const DryRunJobPanel = (props: Props) => {
108107
id: rdiInstanceId,
109108
},
110109
})
111-
dispatch(rdiDryRunJob(rdiInstanceId, JSON.parse(input), yaml.load(job)))
110+
dispatch(rdiDryRunJob(rdiInstanceId, JSON.parse(input), job))
112111
}
113112

114113
const isSelectAvailable = selectedTab === PipelineJobsTabs.Output

redisinsight/ui/src/pages/rdi/pipeline-management/pages/config/Config.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import cx from 'classnames'
66
import { useParams } from 'react-router-dom'
77
import { get, throttle } from 'lodash'
88

9-
import yaml from 'js-yaml'
9+
import { AxiosError } from 'axios'
1010
import { sendPageViewTelemetry, sendEventTelemetry, TelemetryPageView, TelemetryEvent } from 'uiSrc/telemetry'
1111
import { EXTERNAL_LINKS, UTM_MEDIUMS } from 'uiSrc/constants/links'
1212
import { getUtmExternalLink } from 'uiSrc/utils/links'
@@ -17,8 +17,9 @@ import TestConnectionsPanel from 'uiSrc/pages/rdi/pipeline-management/components
1717
import TemplatePopover from 'uiSrc/pages/rdi/pipeline-management/components/template-popover'
1818
import { testConnectionsAction, rdiTestConnectionsSelector, testConnectionsController } from 'uiSrc/slices/rdi/testConnections'
1919
import { appContextPipelineManagement } from 'uiSrc/slices/app/context'
20-
import { isEqualPipelineFile } from 'uiSrc/utils'
20+
import { isEqualPipelineFile, yamlToJson } from 'uiSrc/utils'
2121

22+
import { addErrorNotification } from 'uiSrc/slices/app/notifications'
2223
import styles from './styles.module.scss'
2324

2425
const Config = () => {
@@ -57,8 +58,27 @@ const Config = () => {
5758
}, [isOpenDialog, config, pipelineLoading])
5859

5960
const testConnections = () => {
61+
const onError = (msg: string) => {
62+
dispatch(addErrorNotification({
63+
response: {
64+
data: {
65+
message: (
66+
<>
67+
<EuiText>Config has an invalid structure.</EuiText>
68+
<EuiText>{msg}</EuiText>
69+
</>
70+
)
71+
}
72+
}
73+
} as AxiosError))
74+
}
75+
76+
const JSONValue = yamlToJson(config, onError)
77+
if (!JSONValue) {
78+
return
79+
}
6080
setIsPanelOpen(true)
61-
dispatch(testConnectionsAction(rdiInstanceId, yaml.load(config)))
81+
dispatch(testConnectionsAction(rdiInstanceId, JSONValue))
6282
sendEventTelemetry({
6383
event: TelemetryEvent.RDI_TEST_TARGET_CONNECTIONS_CLICKED,
6484
eventData: {

redisinsight/ui/src/pages/rdi/pipeline-management/pages/job/Job.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useFormikContext } from 'formik'
55
import { get, throttle } from 'lodash'
66
import cx from 'classnames'
77
import { monaco as monacoEditor } from 'react-monaco-editor'
8+
import { AxiosError } from 'axios/index'
89

910
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
1011
import { EXTERNAL_LINKS, UTM_MEDIUMS } from 'uiSrc/constants/links'
@@ -14,10 +15,11 @@ import MonacoYaml from 'uiSrc/components/monaco-editor/components/monaco-yaml'
1415
import DryRunJobPanel from 'uiSrc/pages/rdi/pipeline-management/components/jobs-panel'
1516
import { DSL, KEYBOARD_SHORTCUTS } from 'uiSrc/constants'
1617
import TemplatePopover from 'uiSrc/pages/rdi/pipeline-management/components/template-popover'
17-
import { isEqualPipelineFile, Maybe } from 'uiSrc/utils'
18+
import { isEqualPipelineFile, Maybe, yamlToJson } from 'uiSrc/utils'
1819
import { getUtmExternalLink } from 'uiSrc/utils/links'
1920
import { KeyboardShortcut } from 'uiSrc/components'
2021

22+
import { addErrorNotification } from 'uiSrc/slices/app/notifications'
2123
import styles from './styles.module.scss'
2224

2325
export interface Props {
@@ -33,6 +35,7 @@ const Job = (props: Props) => {
3335

3436
const [isPanelOpen, setIsPanelOpen] = useState<boolean>(false)
3537
const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false)
38+
const [JSONJob, setJSONJob] = useState<object>()
3639
const [shouldOpenDedicatedEditor, setShouldOpenDedicatedEditor] = useState<boolean>(false)
3740

3841
const dispatch = useDispatch()
@@ -66,7 +69,27 @@ const Job = (props: Props) => {
6669
}, [name])
6770

6871
const handleDryRunJob = () => {
72+
const onError = (msg: string) => {
73+
dispatch(addErrorNotification({
74+
response: {
75+
data: {
76+
message: (
77+
<>
78+
<EuiText>{`${name} has an invalid structure.`}</EuiText>
79+
<EuiText>{msg}</EuiText>
80+
</>
81+
)
82+
}
83+
}
84+
} as AxiosError))
85+
}
86+
87+
const JSONValue = yamlToJson(value, onError)
88+
if (!JSONValue) {
89+
return
90+
}
6991
setIsPanelOpen(true)
92+
setJSONJob(JSONValue)
7093
sendEventTelemetry({
7194
event: TelemetryEvent.RDI_TEST_JOB_OPENED,
7295
eventData: {
@@ -234,7 +257,7 @@ const Job = (props: Props) => {
234257
{isPanelOpen && (
235258
<DryRunJobPanel
236259
onClose={() => setIsPanelOpen(false)}
237-
job={value}
260+
job={JSONJob}
238261
/>
239262
)}
240263
</>

redisinsight/ui/src/utils/transformers/transformRdiPipeline.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
import yaml from 'js-yaml'
2-
import { IPipeline, IPipelineJSON, ISources, TestConnectionStatus, TransformResult } from 'uiSrc/slices/interfaces'
1+
import yaml, { YAMLException } from 'js-yaml'
2+
import { IPipeline, IPipelineJSON, ITargets, TestConnectionStatus, TransformResult } from 'uiSrc/slices/interfaces'
3+
4+
export const yamlToJson = (value: string, onError: (e: string) => void) => {
5+
try {
6+
return yaml.load(value) || {}
7+
} catch (e) {
8+
if (e instanceof YAMLException) {
9+
onError(e.reason)
10+
return undefined
11+
}
12+
return undefined
13+
}
14+
}
315

416
export const pipelineToYaml = (pipeline: IPipelineJSON) => ({
517
config: yaml.dump(pipeline.config),
@@ -17,7 +29,7 @@ export const pipelineToJson = ({ config, jobs }: IPipeline): IPipelineJSON => <I
1729
}, {})
1830
})
1931

20-
export const transformConnectionResults = (sources: ISources): TransformResult => {
32+
export const transformConnectionResults = (sources: ITargets): TransformResult => {
2133
const result: TransformResult = { success: [], fail: [] }
2234
if (!sources) {
2335
return result

0 commit comments

Comments
 (0)