Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10-10CG] Reset csrfToken on 403 response for fetchFacilities #34431

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/applications/caregivers/actions/fetchFacilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,27 @@ export const fetchFacilities = async ({
})
.catch(error => {
Sentry.withScope(scope => {
scope.setExtra('error', error);
scope.setExtra('error', error.errors);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This errors object was just showing

{
 errors: [
   [Object]
 ]
}

Which is not helpful. This will instead show the error array which has more detail.

Sentry.captureMessage(content['error--facilities-fetch']);
});

const errorResponse = error?.errors?.[0];

if (
errorResponse?.status === '403' &&
errorResponse?.detail === 'Invalid Authenticity Token'
Copy link
Contributor Author

@coope93 coope93 Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to trigger this for 403s AND when it is an Invalid Authenticity Token, since there are other valid 403 responses we don't want to trigger this logic.

) {
Sentry.withScope(scope => {
coope93 marked this conversation as resolved.
Show resolved Hide resolved
scope.setLevel(Sentry.Severity.Log);
coope93 marked this conversation as resolved.
Show resolved Hide resolved
scope.setExtra('status', errorResponse?.status);
scope.setExtra('detail', errorResponse?.detail);
Sentry.captureMessage(
coope93 marked this conversation as resolved.
Show resolved Hide resolved
'Error in fetchFacilities. Clearing csrfToken in localStorage.',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added new Sentry log so we know when these occur and give us more detail into what's going on.

);
});
localStorage.setItem('csrfToken', '');
}

return {
type: 'SEARCH_FAILED',
errorMessage: 'There was an error fetching the health care facilities.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
Loading