diff --git a/src/__tests__/App.test.tsx b/src/__tests__/App.test.tsx index a418178d9..e0e464156 100644 --- a/src/__tests__/App.test.tsx +++ b/src/__tests__/App.test.tsx @@ -148,6 +148,35 @@ describe('App', () => { expect(document.body).toMatchSnapshot(); }); + it('Should render the correct error message to the console when new_revision is required but missing', async () => { + // Silence console.error for a better console output. + jest.spyOn(console, 'error').mockImplementation(() => {}); + fetchMock.get( + 'begin:https://treeherder.mozilla.org/api/perfcompare/results/', + { + status: 400, + body: JSON.stringify({ + new_revision: ['This field may not be blank.'], + }), + }, + ); + + await router.navigate( + '/compare-results/?baseRev=spam&baseRepo=mozilla-central&framework=2', + ); + render(); + + await screen.findByText(/Error/); + expect(console.error).toHaveBeenCalledWith( + new Error( + 'The comparison cannot be performed because the `new_revision` field is missing. ' + + 'This typically happens when the `mach try perf` push is still being processed by Lando. ' + + 'Please wait a few moments for the push to complete and then refresh the page.', + ), + ); + expect(document.body).toMatchSnapshot(); + }); + it('Should render an error page for compare over time when the treeherder request fails with an error 400', async () => { // Silence console.error for a better console output. We'll check its result later. jest.spyOn(console, 'error').mockImplementation(() => {}); diff --git a/src/__tests__/__snapshots__/App.test.tsx.snap b/src/__tests__/__snapshots__/App.test.tsx.snap index af36bb4d7..59d67bcc0 100644 --- a/src/__tests__/__snapshots__/App.test.tsx.snap +++ b/src/__tests__/__snapshots__/App.test.tsx.snap @@ -695,3 +695,200 @@ exports[`App CompareResults or CompareOverTime loader Should render an error pag `; + +exports[`App CompareResults or CompareOverTime loader Should render the correct error message to the console when new_revision is required but missing 1`] = ` + +
+ + +`; diff --git a/src/logic/treeherder.ts b/src/logic/treeherder.ts index 45fb9bf45..4bc46bfec 100644 --- a/src/logic/treeherder.ts +++ b/src/logic/treeherder.ts @@ -85,15 +85,30 @@ export async function fetchRevisionFromHash( async function fetchFromTreeherder(url: string) { const response = await fetch(url); if (!response.ok) { + const errorText = await response.text(); if (response.status === 400) { - throw new Error( - `Error when requesting treeherder: ${await response.text()}`, - ); - } else { - throw new Error( - `Error when requesting treeherder: (${response.status}) ${response.statusText}`, - ); + let errorJson: Record | null = null; + try { + errorJson = JSON.parse(errorText) as Record; + } catch { + // Fall through to the generic error below if parsing fails + } + + if ( + errorJson?.new_revision?.[0]?.trim() === 'This field may not be blank.' + ) { + throw new Error( + 'The comparison cannot be performed because the `new_revision` field is missing. ' + + 'This typically happens when the `mach try perf` push is still being processed by Lando. ' + + 'Please wait a few moments for the push to complete and then refresh the page.', + ); + } + + throw new Error(`Error when requesting treeherder: ${errorText}`); } + throw new Error( + `Error when requesting treeherder: (${response.status}) ${response.statusText}`, + ); } return response; }