Skip to content

Commit

Permalink
log page props errors to Sentry
Browse files Browse the repository at this point in the history
Wrap workflow API calls in try/catch and log Node errors to Sentry.
  • Loading branch information
eatyourgreens committed Mar 19, 2021
1 parent c982a2e commit a1233b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand All @@ -52,6 +65,7 @@ async function fetchWorkflowCellectStatus(workflow) {
groups = body.groups ?? {}
} catch (error) {
console.error(error)
logNodeError(error)
}
}
return groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})

0 comments on commit a1233b3

Please sign in to comment.