<commit_before>angular.module('mnPromiseHelper', [
  'mnAlertsService',
  'mnHelper'
]).factory('mnPromiseHelper',
  function (mnAlertsService, mnHelper, mnPoll, $timeout) {

    mnPromiseHelper.handleModalAction = function ($scope, promise, $modalInstance) {
      return mnPromiseHelper($scope, promise, $modalInstance)
        .showErrorsSensitiveSpinner()
        .closeFinally()
        .reloadState();
    };

    function mnPromiseHelper(scope, promise, modalInstance) {
      var spinnerName = 'viewLoading';
      var errorsNameOrCallback = 'errors';
      function spinnerCtrl(isLoaded) {
        scope[spinnerName] = isLoaded;
      }
      function errorsCtrl(errors) {
        if (angular.isFunction(errorsNameOrCallback)) {
          errorsNameOrCallback(errors);
        } else {
          scope[errorsNameOrCallback] = errors;
        }
      }
      function hideSpinner() {
        spinnerCtrl(false);
        clearSpinnerTimeout();
      }
      function removeErrors() {
        errorsCtrl(false);
      }
      function setSpinnerName(name) {
        spinnerName = name;
      }
      function setErrorsNameOrCallback(nameOrCallback) {
        errorsNameOrCallback = nameOrCallback;
      }
      function closeModal() {
        modalInstance.close();
      }
      function extractErrors(resp) {
        var errors = resp.data && resp.data.errors && _.keys(resp.data).length === 1 ? resp.data.errors : resp.data;
        return _.isEmpty(errors) ? false : errors;
      }
      var spinnerTimeout;
      function clearSpinnerTimeout() {
        if (spinnerTimeout) {
          $timeout.cancel(spinnerTimeout);
        }
      }
      function enableSpinnerTimeout(timer) {
        spinnerTimeout = $timeout(function () {
          spinnerCtrl(true);
        }, timer);
      }
      function maybeHandleSpinnerWithTimer(timer) {
        if (timer) {
          enableSpinnerTimeout(timer);
          scope.$on("$destroy", clearSpinnerTimeout);
        } else {
          spinnerCtrl(true);
        }
      }
      return {
        applyToScope: function (name) {
          scope[name] = scope[name] || {};
          promise.then(function (value) {
            scope[name] = value;
          });
          return this;
        },
        getPromise: function () {
          return promise;
        },
        onSuccess: function (cb) {
          promise.then(cb);
          return this;
        },
        reloadState: function () {
          promise.then(function () {
            spinnerCtrl(true);
            mnHelper.reloadState();
          });
          return this;
        },
        closeFinally: function () {
          promise['finally'](closeModal);
          return this;
        },
        closeOnSuccess: function () {
          promise.then(closeModal);
          return this;
        },
        showErrorsSensitiveSpinner: function (name, timer) {
          name && setSpinnerName(name);
          maybeHandleSpinnerWithTimer(timer);
          promise.then(null, hideSpinner);
          return this;
        },
        catchErrorsFromSuccess: function (nameOrCallback) {
          nameOrCallback && setErrorsNameOrCallback(nameOrCallback);
          promise.then(function (resp) {
            errorsCtrl(extractErrors(resp));
          });
          return this;
        },
        showSpinner: function (name, timer) {
          name && setSpinnerName(name);
          maybeHandleSpinnerWithTimer(timer);
          promise.then(hideSpinner, hideSpinner);
          return this;
        },
        catchErrors: function (nameOrCallback) {
          nameOrCallback && setErrorsNameOrCallback(nameOrCallback);
          promise.then(removeErrors, function (resp) {
            errorsCtrl(extractErrors(resp));
          });
          return this;
        },
        cleanPollCache: function (key) {
          promise.then(function () {
            mnPoll.cleanCache(key);
          });
          return this;
        },
        catchGlobalErrors: function (errorMessage, timeout) {
          promise.then(null, function (resp) {
            mnAlertsService.formatAndSetAlerts(errorMessage || resp.data, 'danger', timeout);
          });
          return this;
        },
        showGlobalSuccess: function (successMessage, timeout) {
          promise.then(function (resp) {
            mnAlertsService.formatAndSetAlerts(successMessage || resp.data, 'success', timeout);
          });
          return this;
        }
      };
    }

    return mnPromiseHelper;
  });
<commit_msg>MB-12222: return response from extractErrors if reps.data is absent<commit_after>angular.module('mnPromiseHelper', [
  'mnAlertsService',
  'mnHelper'
]).factory('mnPromiseHelper',
  function (mnAlertsService, mnHelper, mnPoll, $timeout) {

    mnPromiseHelper.handleModalAction = function ($scope, promise, $modalInstance) {
      return mnPromiseHelper($scope, promise, $modalInstance)
        .showErrorsSensitiveSpinner()
        .closeFinally()
        .reloadState();
    };

    function mnPromiseHelper(scope, promise, modalInstance) {
      var spinnerName = 'viewLoading';
      var errorsNameOrCallback = 'errors';
      function spinnerCtrl(isLoaded) {
        scope[spinnerName] = isLoaded;
      }
      function errorsCtrl(errors) {
        if (angular.isFunction(errorsNameOrCallback)) {
          errorsNameOrCallback(errors);
        } else {
          scope[errorsNameOrCallback] = errors;
        }
      }
      function hideSpinner() {
        spinnerCtrl(false);
        clearSpinnerTimeout();
      }
      function removeErrors() {
        errorsCtrl(false);
      }
      function setSpinnerName(name) {
        spinnerName = name;
      }
      function setErrorsNameOrCallback(nameOrCallback) {
        errorsNameOrCallback = nameOrCallback;
      }
      function closeModal() {
        modalInstance.close();
      }
      function extractErrors(resp) {
        var errors = resp.data && resp.data.errors && _.keys(resp.data).length === 1 ? resp.data.errors : resp.data || resp ;
        return _.isEmpty(errors) ? false : errors;
      }
      var spinnerTimeout;
      function clearSpinnerTimeout() {
        if (spinnerTimeout) {
          $timeout.cancel(spinnerTimeout);
        }
      }
      function enableSpinnerTimeout(timer) {
        spinnerTimeout = $timeout(function () {
          spinnerCtrl(true);
        }, timer);
      }
      function maybeHandleSpinnerWithTimer(timer) {
        if (timer) {
          enableSpinnerTimeout(timer);
          scope.$on("$destroy", clearSpinnerTimeout);
        } else {
          spinnerCtrl(true);
        }
      }
      return {
        applyToScope: function (name) {
          scope[name] = scope[name] || {};
          promise.then(function (value) {
            scope[name] = value;
          });
          return this;
        },
        getPromise: function () {
          return promise;
        },
        onSuccess: function (cb) {
          promise.then(cb);
          return this;
        },
        reloadState: function () {
          promise.then(function () {
            spinnerCtrl(true);
            mnHelper.reloadState();
          });
          return this;
        },
        closeFinally: function () {
          promise['finally'](closeModal);
          return this;
        },
        closeOnSuccess: function () {
          promise.then(closeModal);
          return this;
        },
        showErrorsSensitiveSpinner: function (name, timer) {
          name && setSpinnerName(name);
          maybeHandleSpinnerWithTimer(timer);
          promise.then(null, hideSpinner);
          return this;
        },
        catchErrorsFromSuccess: function (nameOrCallback) {
          nameOrCallback && setErrorsNameOrCallback(nameOrCallback);
          promise.then(function (resp) {
            errorsCtrl(extractErrors(resp));
          });
          return this;
        },
        showSpinner: function (name, timer) {
          name && setSpinnerName(name);
          maybeHandleSpinnerWithTimer(timer);
          promise.then(hideSpinner, hideSpinner);
          return this;
        },
        catchErrors: function (nameOrCallback) {
          nameOrCallback && setErrorsNameOrCallback(nameOrCallback);
          promise.then(removeErrors, function (resp) {
            errorsCtrl(extractErrors(resp));
          });
          return this;
        },
        cleanPollCache: function (key) {
          promise.then(function () {
            mnPoll.cleanCache(key);
          });
          return this;
        },
        catchGlobalErrors: function (errorMessage, timeout) {
          promise.then(null, function (resp) {
            mnAlertsService.formatAndSetAlerts(errorMessage || resp.data, 'danger', timeout);
          });
          return this;
        },
        showGlobalSuccess: function (successMessage, timeout) {
          promise.then(function (resp) {
            mnAlertsService.formatAndSetAlerts(successMessage || resp.data, 'success', timeout);
          });
          return this;
        }
      };
    }

    return mnPromiseHelper;
  });

{'fraction_non_alphanumeric': 0.10803384987361249, 'fraction_numerical': 0.0007693153093746566, 'mean_word_length': 2.009259259259259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2648683', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2401, 'n_words': 435}