diff --git a/src/applications/caregivers/actions/fetchFacilities.js b/src/applications/caregivers/actions/fetchFacilities.js index 0e26f5104305..2f4f91ec3046 100644 --- a/src/applications/caregivers/actions/fetchFacilities.js +++ b/src/applications/caregivers/actions/fetchFacilities.js @@ -125,10 +125,27 @@ export const fetchFacilities = async ({ }) .catch(error => { Sentry.withScope(scope => { - scope.setExtra('error', error); + scope.setExtra('error', error.errors); Sentry.captureMessage(content['error--facilities-fetch']); }); + const errorResponse = error?.errors?.[0]; + + if ( + errorResponse?.status === '403' && + errorResponse?.detail === 'Invalid Authenticity Token' + ) { + Sentry.withScope(scope => { + scope.setLevel(Sentry.Severity.Log); + scope.setExtra('status', errorResponse?.status); + scope.setExtra('detail', errorResponse?.detail); + Sentry.captureMessage( + 'Error in fetchFacilities. Clearing csrfToken in localStorage.', + ); + }); + localStorage.setItem('csrfToken', ''); + } + return { type: 'SEARCH_FAILED', errorMessage: 'There was an error fetching the health care facilities.', diff --git a/src/applications/caregivers/tests/unit/actions/fetchFacilities.unit.spec.js b/src/applications/caregivers/tests/unit/actions/fetchFacilities.unit.spec.js index d534ea84c308..109031890ba2 100644 --- a/src/applications/caregivers/tests/unit/actions/fetchFacilities.unit.spec.js +++ b/src/applications/caregivers/tests/unit/actions/fetchFacilities.unit.spec.js @@ -239,5 +239,32 @@ describe('CG fetchFacilities action', () => { expect(apiRequestStub.callCount).to.equal(1); }); }); + + it('should log to sentry and reset csrfToken on 403 Invalid Authenticity Token error', async () => { + expect(localStorage.getItem('csrfToken')).to.eql('my-token'); + const invalidAuthenticityTokenResponse = { + errors: [{ status: '403', detail: 'Invalid Authenticity Token' }], + }; + apiRequestStub.rejects(invalidAuthenticityTokenResponse); + + const response = await fetchFacilities({ long, lat }); + expect(response).to.eql({ + type: 'SEARCH_FAILED', + errorMessage: 'There was an error fetching the health care facilities.', + }); + + expect(sentrySpy.called).to.be.true; + expect(sentrySpy.firstCall.args[0]).to.equal( + 'Error fetching Lighthouse VA facilities', + ); + expect(sentrySpy.secondCall.args[0]).to.equal( + 'Error in fetchFacilities. Clearing csrfToken in localStorage.', + ); + expect(localStorage.getItem('csrfToken')).to.eql(''); + + await waitFor(() => { + expect(apiRequestStub.callCount).to.equal(1); + }); + }); }); });