2020-03-27 13:59:38 -07:00
|
|
|
import { defineMessages } from 'react-intl';
|
|
|
|
|
2022-03-28 13:29:13 -07:00
|
|
|
import { httpErrorMessages } from 'soapbox/utils/errors';
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
const messages = defineMessages({
|
|
|
|
unexpectedTitle: { id: 'alert.unexpected.title', defaultMessage: 'Oops!' },
|
|
|
|
unexpectedMessage: { id: 'alert.unexpected.message', defaultMessage: 'An unexpected error occurred.' },
|
|
|
|
});
|
|
|
|
|
|
|
|
export const ALERT_SHOW = 'ALERT_SHOW';
|
|
|
|
export const ALERT_DISMISS = 'ALERT_DISMISS';
|
|
|
|
export const ALERT_CLEAR = 'ALERT_CLEAR';
|
|
|
|
|
2021-10-05 14:13:42 -07:00
|
|
|
const noOp = () => {};
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
export function dismissAlert(alert) {
|
|
|
|
return {
|
|
|
|
type: ALERT_DISMISS,
|
|
|
|
alert,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function clearAlert() {
|
|
|
|
return {
|
|
|
|
type: ALERT_CLEAR,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2020-09-29 16:55:05 -07:00
|
|
|
export function showAlert(title = messages.unexpectedTitle, message = messages.unexpectedMessage, severity = 'info') {
|
2020-03-27 13:59:38 -07:00
|
|
|
return {
|
|
|
|
type: ALERT_SHOW,
|
|
|
|
title,
|
|
|
|
message,
|
2020-09-29 16:55:05 -07:00
|
|
|
severity,
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function showAlertForError(error) {
|
2022-03-28 07:36:43 -07:00
|
|
|
return (dispatch, _getState) => {
|
2021-10-05 14:13:42 -07:00
|
|
|
if (error.response) {
|
|
|
|
const { data, status, statusText } = error.response;
|
|
|
|
|
|
|
|
if (status === 502) {
|
|
|
|
return dispatch(showAlert('', 'The server is down', 'error'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status === 404 || status === 410) {
|
|
|
|
// Skip these errors as they are reflected in the UI
|
|
|
|
return dispatch(noOp);
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:29:13 -07:00
|
|
|
let message = statusText;
|
2021-10-05 14:13:42 -07:00
|
|
|
|
|
|
|
if (data.error) {
|
|
|
|
message = data.error;
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:29:13 -07:00
|
|
|
if (!message) {
|
|
|
|
message = httpErrorMessages.find((httpError) => httpError.code === status)?.description;
|
|
|
|
}
|
|
|
|
|
2022-03-28 07:36:43 -07:00
|
|
|
return dispatch(showAlert('', message, 'error'));
|
2021-10-05 14:13:42 -07:00
|
|
|
} else {
|
|
|
|
console.error(error);
|
|
|
|
return dispatch(showAlert(undefined, undefined, 'error'));
|
2021-05-21 18:16:09 -07:00
|
|
|
}
|
2021-10-05 14:13:42 -07:00
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|