闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
813 B

  1. import { showAlert } from '../actions/alerts';
  2. const defaultFailSuffix = 'FAIL';
  3. export default function errorsMiddleware() {
  4. return ({ dispatch }) => next => action => {
  5. if (action.type && !action.skipAlert) {
  6. const isFail = new RegExp(`${defaultFailSuffix}$`, 'g');
  7. if (action.type.match(isFail)) {
  8. if (action.error.response) {
  9. const { data, status, statusText } = action.error.response;
  10. let message = statusText;
  11. let title = `${status}`;
  12. if (data.error) {
  13. message = data.error;
  14. }
  15. dispatch(showAlert(title, message));
  16. } else {
  17. console.error(action.error);
  18. dispatch(showAlert('Oops!', 'An unexpected error occurred.'));
  19. }
  20. }
  21. }
  22. return next(action);
  23. };
  24. };