Skip to content

Commit

Permalink
handle uniconfig install/uninstall api errors (#1639) (#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
soson authored Sep 25, 2024
1 parent 67e1e04 commit 87c2ddb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ import WorkflowListModal from './workflow-list-modal';
import LocationMapModal, { LocationModal } from '../../components/edit-location-map-modal';
import { LocationData } from '../create-device/create-device-page';

type UniconfigErrorItem = {
'error-tag': string; // eslint-disable-line @typescript-eslint/naming-convention
'error-info': Record<string, string>; // eslint-disable-line @typescript-eslint/naming-convention
'error-message': string; // eslint-disable-line @typescript-eslint/naming-convention
'error-type': string; // eslint-disable-line @typescript-eslint/naming-convention
};

type UniconfigError = {
code: number;
message: {
errors: {
error: UniconfigErrorItem[];
};
};
};

function parseErrorMessage(msg: string): string {
try {
const parsedError: UniconfigError = JSON.parse(msg);

return parsedError.message.errors.error.map((e) => e['error-message']).join('\n');
} catch (e) {
return 'unknown error';
}
}

const DEVICES_QUERY = gql`
query Devices(
$labels: [String!]
Expand Down Expand Up @@ -508,14 +534,20 @@ const DeviceList: VoidFunctionComponent = () => {
content: 'Device uninstalled successfuly',
});
}
if (res.error) {
addToastNotification({
type: 'error',
title: 'Error',
content: 'Uninstallation failed',
});
if (res.error != null) {
const message = res.error.graphQLErrors.at(0)?.message ?? '';
throw new Error(parseErrorMessage(message));
}
})
.catch((e) => {
addToastNotification({
type: 'error',
title: 'Error',
timeout: 10000,
content: `Uninstallation failed:\n\n
${e}`,
});
})
.finally(() => {
setInstallLoadingMap((m) => {
return {
Expand Down Expand Up @@ -589,14 +621,18 @@ const DeviceList: VoidFunctionComponent = () => {
});
}
if (res.error != null) {
throw new Error(res.error?.message);
const message = parseErrorMessage(res.error.graphQLErrors[0].message);
throw new Error(message);
}
})
.catch(() => {
.catch((e) => {
addToastNotification({
type: 'error',
title: 'Error',
content: 'Installation failed',
timeout: 10,
content: `Installation failed
${e}
`,
});
})
.finally(() => {
Expand Down Expand Up @@ -628,7 +664,8 @@ const DeviceList: VoidFunctionComponent = () => {
})
.then((res) => {
if (res.error != null || res.data == null) {
throw new Error(res.error?.message ?? 'Problem with bulk installation of devices');
const bulkErrors = res.error?.graphQLErrors.map((e) => parseErrorMessage(e.message)) ?? [];
throw new Error(bulkErrors.join('\n'));
}

if (res.data?.deviceInventory.bulkInstallDevices.installedDevices.length === 0) {
Expand All @@ -641,11 +678,13 @@ const DeviceList: VoidFunctionComponent = () => {
content: 'Devices installed successfuly',
});
})
.catch(() => {
.catch((e) => {
addToastNotification({
type: 'error',
title: 'Error',
content: 'Bulk installation of devices has failed',
timeout: 10,
content: `Bulk installation of devices has failed
${e.message}`,
});
})
.finally(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Heading, Flex, keyframes, Icon, Box } from '@chakra-ui/react';
import React, { FC, useState, useEffect, useRef } from 'react';
import FeatherIcon from 'feather-icons-react';

const DEFAULT_TIMEOUT_DISMISS = 3000; // default timeout in which notification will automatically dismiss (ms)\
const DEFAULT_TIMEOUT_DISMISS = 5000; // default timeout in which notification will automatically dismiss (ms)\

const slideIn = keyframes`
from {
Expand Down

0 comments on commit 87c2ddb

Please sign in to comment.