Skip to content
Draft
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
29 changes: 29 additions & 0 deletions src/__tests__/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<App />);

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(() => {});
Expand Down
17 changes: 14 additions & 3 deletions src/logic/treeherder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,21 @@ 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()}`,
);
try {
const errorJson = JSON.parse(errorText) as Record<string, string>;
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.',
);
}
} catch {
// Fall through to the generic error if the response is not JSON or doesn't have the expected structure
}
throw new Error(`Error when requesting treeherder here: ${errorText}`);
} else {
throw new Error(
`Error when requesting treeherder: (${response.status}) ${response.statusText}`,
Expand Down