Skip to content

Commit

Permalink
Fixes Bad reporting 500 Status Code (#3541)
Browse files Browse the repository at this point in the history
* Fixed 500 error reporting

* Fixed 500 error reporting and added tests

* Lint Test Fix

* Test Fixes
  • Loading branch information
jain-naman-sf authored Nov 3, 2023
1 parent bc54d71 commit 3347a2a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions locales_dev/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"Share Link to Installation Job": "Share Link to Installation Job",
"Show Logs": "Show Logs",
"Skipped": "Skipped",
"Something went wrong. Please try again later.": "Something went wrong. Please try again later.",
"Start Pre-Install Validation": "Start Pre-Install Validation",
"Start Pre-Install Validation on Scratch Org": "Start Pre-Install Validation on Scratch Org",
"Steps": "Steps",
Expand Down
9 changes: 9 additions & 0 deletions src/js/components/apiErrors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ const ErrorToast = ({
doRemoveError: typeof removeError;
}) => {
const { t } = useTranslation();

if (
error.message &&
typeof error.message === 'string' &&
/<[a-z][\s\S]*>/i.test(error.message)
) {
error.message = t('Something went wrong. Please try again later.');
}

return (
<Toast
labels={{
Expand Down
3 changes: 3 additions & 0 deletions src/js/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ const apiFetch = (
msg = body.non_field_errors;
}
}
if (response.status > 500 && response.status <= 600) {
msg = 'Something went wrong. Please try again later.';
}
dispatch(addError(msg));
const error: ApiError = new Error(msg);
error.response = response;
Expand Down
17 changes: 17 additions & 0 deletions test/js/components/apiErrors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('<Errors />', () => {
return { getByText };
};

const setupforRawHTML = () => {
const errors = [
{ id: 'err1', message: '<html><div>This is Error</div></html>' },
];
const { getByText } = render(
<Errors errors={errors} doRemoveError={doRemoveError} />,
);
return { getByText };
};

test('calls window.location.reload on link click', () => {
const { getByText } = setup();

Expand All @@ -24,6 +34,13 @@ describe('<Errors />', () => {

expect(window.location.reload).toHaveBeenCalledTimes(1);
});
test('for raw html error', () => {
const { getByText } = setupforRawHTML();

expect(
getByText('Something went wrong. Please try again later.'),
).toBeVisible();
});

test('calls doRemoveError on close click', () => {
const { getByText } = setup();
Expand Down
8 changes: 8 additions & 0 deletions test/js/utils/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe('apiFetch', () => {
});

describe('error', () => {
test('throws Error for status code greater than 500', () => {
fetchMock.getOnce('/test/url/', { status: 503, body: {} });

expect.assertions(1);
return expect(apiFetch('/test/url/', dispatch)).rejects.toThrow(
'Something went wrong. Please try again later.',
);
});
test('throws Error without response', () => {
fetchMock.getOnce('/test/url/', { status: 500, body: {} });

Expand Down

0 comments on commit 3347a2a

Please sign in to comment.