Skip to content

Commit

Permalink
refactor: extract generic function to parse requests errors
Browse files Browse the repository at this point in the history
  • Loading branch information
csm-thu committed Jul 4, 2022
1 parent 89f4f14 commit bc50bd0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/state/sagas/app/FetchInitialData/FetchInitialData.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DATASET_ACTIONS_KEY } from '../../../commons/DatasetConstants';
import { WORKSPACE_ACTIONS_KEY } from '../../../commons/WorkspaceConstants';
import { SOLUTION_ACTIONS_KEY } from '../../../commons/SolutionConstants';
import { getFirstScenarioMaster } from '../../../../utils/SortScenarioListUtils';
import { parseError } from '../../../../utils/ErrorsUtils';

const selectSolutionIdFromCurrentWorkspace = (state) => state.workspace.current.data.solution.solutionId;
const selectScenarioList = (state) => state.scenario.list.data;
Expand Down Expand Up @@ -60,31 +61,7 @@ export function* fetchAllInitialData(action) {
status: STATUSES.SUCCESS,
});
} catch (error) {
let errorDetails;
if (error?.response?.data) {
errorDetails = error.response.data;
} else if (error.message === 'Network Error') {
errorDetails = {
title: 'Network error',
status: null,
detail: 'No response received. If the problem persists, please contact your administrator.',
};
} else if (error.request) {
errorDetails = error.request;
errorDetails = {
title: error.request.statusText,
status: error.request.status,
detail: error.request.response,
};
} else {
errorDetails = {
title: 'Unexpected error',
status: null,
detail:
'An unexpected error happened during the app initialization. ' +
'If the problem persists, please contact your administrator',
};
}
const errorDetails = parseError(error);
yield put({
type: APPLICATION_ACTIONS_KEY.SET_APPLICATION_STATUS,
status: STATUSES.ERROR,
Expand Down
44 changes: 44 additions & 0 deletions src/utils/ErrorsUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Cosmo Tech.
// Licensed under the MIT license.

const _isString = (value) => {
return typeof value === 'string' || value instanceof String;
};

export const parseError = (error) => {
if (error?.response?.data) {
const errorData = error.response.data;
if (_isString(errorData)) {
return {
title: error.response.statusText,
status: error.response.status,
detail: errorData,
};
}
return {
title: errorData.title || errorData.statusText || error.response.statusText,
status: errorData.status || errorData.code || error.response.status,
detail: errorData.detail || errorData.message,
};
} else if (error?.message === 'Network Error' || error?.title === 'Network Error') {
return {
title: 'Network error',
status: null,
detail:
'No response received, please check your network settings. If the problem persists, please contact ' +
'your administrator.',
};
} else if (error?.request) {
return {
title: error.request.statusText,
status: error.request.status,
detail: error.request.response,
};
}

return {
title: 'Unknown error',
status: null,
detail: 'An unknown error happened. If the problem persists, please contact your administrator.',
};
};

0 comments on commit bc50bd0

Please sign in to comment.