From 8baccba0669822a126d8e549a2cd0b0d3d2bda0a Mon Sep 17 00:00:00 2001 From: Justin Hynes Date: Fri, 26 Jun 2020 14:41:57 -0400 Subject: [PATCH] Small update to error handling in postDemographics call --- .../demographics/data/service.js | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/account-settings/demographics/data/service.js b/src/account-settings/demographics/data/service.js index 965d7be..bf2041a 100644 --- a/src/account-settings/demographics/data/service.js +++ b/src/account-settings/demographics/data/service.js @@ -16,20 +16,8 @@ export async function postDemographics(userId) { ({ data } = await getAuthenticatedHttpClient() .post(requestUrl, commitValues) .catch((error) => { - data = { - user: userId, - gender: null, - gender_description: null, - income: null, - learner_education_level: null, - parent_education_level: null, - military_history: null, - work_status: null, - work_status_description: null, - current_work_sector: null, - future_work_sector: null, - user_ethnicity: [] - } + const apiError = createDemographicsError(error); + throw apiError; })); return convertData(data, FROM); @@ -88,16 +76,25 @@ export async function patchDemographics(userId, commitValues) { ({ data } = await getAuthenticatedHttpClient() .patch(requestUrl, convertedCommitValues) .catch((error) => { - // If there was an error making the PATCH call then we create an apiError object containing a 'demographicsError' - // fieldError. The content of the error itself isn't particularly important. This will trigger the - // `renderDemographicsServiceIssueWarningMessage()` (DemographicsSection.jsx) to display an Alert to let the - // end-user know that there may be an issue communicating with the Demographics service. - const apiError = Object.create(error); - apiError.fieldErrors = { - demographicsError: error.customAttributes.httpErrorType - } + const apiError = createDemographicsError(error); throw apiError; })); return convertData(data, FROM); } + +/** + * If there was an error making the PATCH call then we create an apiError object containing a 'demographicsError' + * fieldError. The content of the error itself isn't particularly important. This will trigger the + * `renderDemographicsServiceIssueWarningMessage()` (DemographicsSection.jsx) to display an Alert to let the + * end-user know that there may be an issue communicating with the Demographics service. + * + * @param {Error} error + */ +export function createDemographicsError(error) { + const apiError = Object.create(error); + apiError.fieldErrors = { + demographicsError: error.customAttributes.httpErrorType + } + return apiError; +}