Skip to content

Commit

Permalink
feat: Adding human readable 403 error access restricted
Browse files Browse the repository at this point in the history
Signed-off-by: Farhaan Bukhsh <[email protected]>
  • Loading branch information
farhaanbukhsh committed Dec 27, 2024
1 parent dc0ba6a commit bac3bee
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/course-outline/page-alerts/PageAlerts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,34 @@ const PageAlerts = ({
const renderApiErrors = () => {
let errorList = Object.entries(errors).filter(obj => obj[1] !== null).map(([k, v]) => {
switch (v.type) {
case API_ERROR_TYPES.serverError:
case API_ERROR_TYPES.serverError: {
let description = (
<Truncate>
{v.data || intl.formatMessage(messages.serverErrorAlertBody)}
</Truncate>
);
let alertTitle = intl.formatMessage(messages.serverErrorAlert);
if (v.status === 403) {
description = intl.formatMessage(messages.forbiddenAlertBody, {
LMS: (
<Hyperlink
destinationdestination={`${getConfig().LMS_BASE_URL}`}
target="_blank"
showLaunchIcon={false}
>
{intl.formatMessage(messages.forbiddenAlertLmsUrl)}
</Hyperlink>
),
});
alertTitle = intl.formatMessage(messages.forbiddenAlert);
}
return {
key: k,
desc: v.data || intl.formatMessage(messages.serverErrorAlertBody),
title: intl.formatMessage(messages.serverErrorAlert),
desc: description,
title: alertTitle,
dismissible: v.dismissible,
};
}
case API_ERROR_TYPES.networkError:
return {
key: k,
Expand Down Expand Up @@ -378,7 +399,7 @@ const PageAlerts = ({
dismissError={() => dispatch(dismissError(msgObj.key))}
>
<Alert.Heading>{msgObj.title}</Alert.Heading>
{msgObj.desc && <Truncate lines={2}>{msgObj.desc}</Truncate>}
{msgObj.desc}
</ErrorAlert>
) : (
<Alert
Expand All @@ -387,7 +408,7 @@ const PageAlerts = ({
key={msgObj.key}
>
<Alert.Heading>{msgObj.title}</Alert.Heading>
{msgObj.desc && <Truncate lines={2}>{msgObj.desc}</Truncate>}
{msgObj.desc}
</Alert>
)
))
Expand Down
25 changes: 25 additions & 0 deletions src/course-outline/page-alerts/PageAlerts.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,29 @@ describe('<PageAlerts />', () => {
expect(queryByText('some error')).toBeInTheDocument();
expect(queryByText('some unknown error')).toBeInTheDocument();
});

it('renders forbidden api error alerts', async () => {
const { queryByText } = renderComponent({
...pageAlertsData,
errors: {
outlineIndexApi: {
data: 'some error', status: 403, type: API_ERROR_TYPES.serverError, dismissable: true,
},
},
});
expect(queryByText(messages.forbiddenAlert.defaultMessage)).toBeInTheDocument();
expect(queryByText(messages.forbiddenAlertBody.defaultMessage)).toBeInTheDocument();
});

it('renders api error alerts when status is not 403', async () => {
const { queryByText } = renderComponent({
...pageAlertsData,
errors: {
outlineIndexApi: {
data: 'some error', status: 500, type: API_ERROR_TYPES.serverError, dismissable: true,
},
},
});
expect(queryByText('some error')).toBeInTheDocument();
});
});
15 changes: 15 additions & 0 deletions src/course-outline/page-alerts/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ const messages = defineMessages({
defaultMessage: 'Network error',
description: 'Generic network error alert.',
},
forbiddenAlert: {
id: 'course-authoring.course-outline.page-alert.forbidden.title',
defaultMessage: 'Access Restricted',
description: 'Forbidden(403) alert title',
},
forbiddenAlertBody: {
id: 'course-authoring.course-outline.page-alert.forbidden.body',
defaultMessage: 'It looks like you’re trying to access a page you don’t have permission to view. Contact your admin if you think this is a mistake, or head back to the {LMS}.',
description: 'Forbidden(403) alert body',
},
forbiddenAlertLmsUrl: {
id: 'course-authoring.course-outline.page-alert.lms',
defaultMessage: 'LMS',
description: 'LMS base redirection url',
},
});

export default messages;

0 comments on commit bac3bee

Please sign in to comment.