From a1233b3d320b54ee43b46053f2b689703b8fd930 Mon Sep 17 00:00:00 2001 From: Jim O'Donnell Date: Mon, 22 Feb 2021 13:21:27 +0000 Subject: [PATCH] log page props errors to Sentry Wrap workflow API calls in try/catch and log Node errors to Sentry. --- .../fetchWorkflowsHelper.js | 40 +++++++++++++------ .../fetchWorkflowsHelper.spec.js | 28 ++++++++----- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.js b/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.js index 0b6488ffd34..ac9714410cf 100644 --- a/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.js +++ b/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.js @@ -1,16 +1,22 @@ import { panoptes } from '@zooniverse/panoptes-js' import fetch from 'node-fetch' +import { logNodeError } from '@helpers/logger' + async function fetchWorkflowData (activeWorkflows, env) { - const query = { - complete: false, - env, - fields: 'completeness,display_name,grouped', - id: activeWorkflows.join(',') + try { + const query = { + complete: false, + env, + fields: 'completeness,display_name,grouped', + id: activeWorkflows.join(',') + } + const response = await panoptes.get('/workflows', query) + return response.body.workflows + } catch (error) { + logNodeError(error) + throw error } - const response = await panoptes.get('/workflows', query) - const { workflows } = response.body - return workflows } async function fetchSubjectSetData(subjectSetIDs, env) { @@ -25,21 +31,28 @@ async function fetchSubjectSetData(subjectSetIDs, env) { await Promise.allSettled(subject_sets.map(subjectSet => fetchPreviewImage(subjectSet, env))) } catch (error) { console.error(error) + logNodeError(error) } return subject_sets } -function fetchDisplayNames (language, activeWorkflows, env) { - return panoptes - .get('/translations', { +async function fetchDisplayNames (language, activeWorkflows, env) { + let displayNames = {} + try { + const response = await panoptes.get('/translations', { env, fields: 'strings,translated_id', language, 'translated_id': activeWorkflows.join(','), 'translated_type': 'workflow' }) - .then(response => response.body.translations) - .then(createDisplayNamesMap) + const { translations } = response.body + displayNames = createDisplayNamesMap(translations) + } catch (error) { + logNodeError(error) + throw error + } + return displayNames } async function fetchWorkflowCellectStatus(workflow) { @@ -52,6 +65,7 @@ async function fetchWorkflowCellectStatus(workflow) { groups = body.groups ?? {} } catch (error) { console.error(error) + logNodeError(error) } } return groups diff --git a/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.spec.js b/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.spec.js index 5271be2cedc..7cb8e71ee06 100644 --- a/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.spec.js +++ b/packages/app-project/src/helpers/fetchWorkflowsHelper/fetchWorkflowsHelper.spec.js @@ -219,29 +219,37 @@ describe('Helpers > fetchWorkflowsHelper', function () { }) describe(`when there's an error`, function () { + let workflows + it('should allow the error to be thrown for the consumer to handle', async function () { - const error = { - message: 'oh dear. oh dear god' - } + let thrownError + const mockError = new Error('oh dear. oh dear god') const scope = nock('https://panoptes-staging.zooniverse.org/api') .get('/translations') .query(true) - .replyWithError(error) + .replyWithError(mockError) .get('/workflows') .query(true) .reply(200, { workflows: WORKFLOWS }) + .get('/subject_sets') + .query(true) + .reply(200, { + subject_sets: [ + subjectSet('1'), + subjectSet('2'), + subjectSet('3') + ] + }) try { - await fetchWorkflowsHelper('en', ['1', '2'], '2') - expect.fail() + workflows = await fetchWorkflowsHelper('en', ['1', '2'], '2') } catch (error) { - expect(error).to.deep.equal({ - ...error, - response: undefined - }) + thrownError = error } + expect(thrownError).to.deep.equal(mockError) + expect(workflows).to.be.undefined() }) }) })