闭社主体 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.

33 lines
901 B

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