<commit_before>angular.module('lampost_remote', []).service('lpRemote', ['$timeout', '$http', '$q', 'lmLog', 'lpEvent', 'lpDialog',
  function ($timeout, $http, $q, lmLog, lpEvent, lpDialog) {

    var sessionId = '';
    var connectEndpoint = '';
    var connected = false;
    var loadingTemplate;
    var waitCount = 0;
    var waitDialogId = null;
    var services = {};
    var resourceRoot = '/';
    var reconnectTemplate = '<div class="modal"><div class="modal-dialog"><div class="modal-content">' +
      '<div class="modal-header"> <h3>Reconnecting To Server</h3></div><div class="modal-body">' +
      '<p>Reconnecting in {{time}} seconds.</p></div><div class="modal-footer">' +
      '<button class="btn btn-primary" ng-click="reconnectNow()">Reconnect Now</button></div></div></div></div>';

    function serverRequest(resource, data) {
      return $http({method: 'POST',
        url: resourceRoot + resource,
        data: data || {},
        headers: sessionId ? {'X-Lampost-Session': sessionId} : null}).success(lpEvent.dispatchMap).error(linkFailure);
    }

    function resourceRequest(resource, data) {
      if (waitCount == 0) {
        waitDialogId = lpDialog.show({template: loadingTemplate, noEscape: true});
      }
      waitCount++;

      function checkWait() {
        waitCount--;
        if (waitCount == 0) {
          lpDialog.close(waitDialogId);
          waitDialogId = null;
        }
      }

      return rawRequest(resource, data, checkWait);
    }

    function rawRequest(resource, data, checkWait) {
      var deferred = $q.defer();
      $http({method: 'POST', url: resourceRoot + resource, data: data || {}, headers: {'X-Lampost-Session': sessionId}})
        .success(function (data) {
          checkWait && checkWait();
          deferred.resolve(data);
        }).error(function (data, status) {
          checkWait && checkWait();
          if (status === 409 || status === 400) {
            var errorResult = {id: 'Error', raw: data, text: data};
            var colon_ix = data.indexOf(':');
            if (colon_ix > 0) {
              var space_ix = data.indexOf(' ');
              if (space_ix == -1 || colon_ix < space_ix) {
                errorResult.id = data.substring(0, colon_ix);
                errorResult.text = $.trim(data.substring(colon_ix + 1));
              }
            }
            deferred.reject(errorResult);
          } else if (status === 403) {
            lpDialog.showOk("Denied", "You do not have permission for that action");
          } else if (status) {
            lpDialog.showOk("Server Error: " + status, data);
          }
        });

      return deferred.promise;
    }

    function linkFailure(data, status) {
      if (!connected) {
        return;
      }
      if (status >= 500) {
        lpEvent.dispatch('server_error');
        return;
      }
      connected = false;
      lmLog.log("Link stopped: " + status);
      $timeout(function () {
        if (!connected && !window.windowClosing) {
          lpDialog.show({template: reconnectTemplate, controller: ReconnectCtrl, noEscape: true});
        }
      }, 50);
    }<commit_msg>Do reconnect on non-internal server errors.<commit_after>angular.module('lampost_remote', []).service('lpRemote', ['$timeout', '$http', '$q', 'lmLog', 'lpEvent', 'lpDialog',
  function ($timeout, $http, $q, lmLog, lpEvent, lpDialog) {

    var sessionId = '';
    var connectEndpoint = '';
    var connected = false;
    var loadingTemplate;
    var waitCount = 0;
    var waitDialogId = null;
    var services = {};
    var resourceRoot = '/';
    var reconnectTemplate = '<div class="modal"><div class="modal-dialog"><div class="modal-content">' +
      '<div class="modal-header"> <h3>Reconnecting To Server</h3></div><div class="modal-body">' +
      '<p>Reconnecting in {{time}} seconds.</p></div><div class="modal-footer">' +
      '<button class="btn btn-primary" ng-click="reconnectNow()">Reconnect Now</button></div></div></div></div>';

    function serverRequest(resource, data) {
      return $http({method: 'POST',
        url: resourceRoot + resource,
        data: data || {},
        headers: sessionId ? {'X-Lampost-Session': sessionId} : null}).success(lpEvent.dispatchMap).error(linkFailure);
    }

    function resourceRequest(resource, data) {
      if (waitCount == 0) {
        waitDialogId = lpDialog.show({template: loadingTemplate, noEscape: true});
      }
      waitCount++;

      function checkWait() {
        waitCount--;
        if (waitCount == 0) {
          lpDialog.close(waitDialogId);
          waitDialogId = null;
        }
      }

      return rawRequest(resource, data, checkWait);
    }

    function rawRequest(resource, data, checkWait) {
      var deferred = $q.defer();
      $http({method: 'POST', url: resourceRoot + resource, data: data || {}, headers: {'X-Lampost-Session': sessionId}})
        .success(function (data) {
          checkWait && checkWait();
          deferred.resolve(data);
        }).error(function (data, status) {
          checkWait && checkWait();
          if (status === 409 || status === 400) {
            var errorResult = {id: 'Error', raw: data, text: data};
            var colon_ix = data.indexOf(':');
            if (colon_ix > 0) {
              var space_ix = data.indexOf(' ');
              if (space_ix == -1 || colon_ix < space_ix) {
                errorResult.id = data.substring(0, colon_ix);
                errorResult.text = $.trim(data.substring(colon_ix + 1));
              }
            }
            deferred.reject(errorResult);
          } else if (status === 403) {
            lpDialog.showOk("Denied", "You do not have permission for that action");
          } else if (status) {
            lpDialog.showOk("Server Error: " + status, data);
          }
        });

      return deferred.promise;
    }

    function linkFailure(data, status) {
      if (!connected) {
        return;
      }
      if (status == 500) {
        lpEvent.dispatch('server_error');
        return;
      }
      connected = false;
      lmLog.log("Link stopped: " + status);
      $timeout(function () {
        if (!connected && !window.windowClosing) {
          lpDialog.show({template: reconnectTemplate, controller: ReconnectCtrl, noEscape: true});
        }
      }, 50);
    }
{'fraction_non_alphanumeric': 0.15701171256726812, 'fraction_numerical': 0.0072807850585628366, 'mean_word_length': 2.390021459227468, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 42, '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': '4096812', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1805, 'n_words': 419}