auth0-extensions/auth0-delegated-administration-extension

View on GitHub
client/middlewares/normalizeErrorMiddleware.js

Summary

Maintainability
A
1 hr
Test Coverage
export default function normalizeErrorMiddleware() {
  return () => next => action => {
    if (action && action.type.endsWith('_REJECTED') && action.payload) {
      // Try to get the default error message from the response.
      let message = action.payload.statusText || action.payload.status || 'Unknown Server Error';

      const status = (action.payload.response && action.payload.response.status) || 500;
      // Maybe some data is available.
      let error = action.payload.data && action.payload.data.message;
      if (!error) {
        error = action.payload.response && action.payload.response.data && action.payload.response.data.message;
      }

      if (error) {
        message = error.message || error;
      }

      action.errorData = {
        type: action.type.replace('_REJECTED', ''),
        message,
        status
      };
    }

    next(action);
  };
}