<commit_before>var errors = require('./errors');
var request = require('./request');
var package = require('../package');
var btoa = require('btoa');

module.exports = invoker;

var toString = Object.prototype.toString;

function invoker(portalURL, path, auth, payload) {
  var req = request.post(portalURL + path);

  req.parse && req.parse(request.parse[ 'application/json' ]);
  req.set({"Accept-Encoding" : ""});

  if (arguments.length === 3) {
    payload = auth;
    auth = null;
  }

  if (auth != null) {
    req.set('Authorization', 'Basic ' + btoa(auth.login + ':' + auth.password));
  }

  req.set({ 'User-Agent': 'liferay-core-connector v' + package.version });

  req.redirects(0);

  req.type('json').send(payload);

  return req;
}

invoker.knownErrors = [];

function knownError(test, act) {
  var re = test;
  var exc = act;
  var status = act;

  if (toString.call(test) === '[object RegExp]') {
    test = function (input) {
      return re.test(input);
    };
  }

  if (typeof act === 'number') {
    act = function (res) {
      res.setStatusProperties(el.status);
    };
  }
  else if (act.prototype instanceof Error) {
    act = function (res) {
      throw new exc(res.exception);
    };
  }

  invoker.knownErrors.push({
    test: test,
    act: act
  });
}

knownError(/no *such/i, errors.NotFound);
knownError(/principal *exception/i, errors.PrincipalException);
knownError(/please *sign/i, errors.Unauthorized);
knownError(/authenticated *access/i, errors.Unauthorized);

invoker.parseLiferayResponse = parseLiferayResponse;
invoker.retrieveBuildNumber = retrieveBuildNumber;
invoker.getBody = getBody;

function getBody(res) {
  return res.body;
}

function retrieveBuildNumber(res) {
  var header = res.headers[ 'liferay-portal' ];

  if (!header) {
    throw res;
  }

  var match = header.match(/Build\s+(\d+)/i);

  if (!match) {
    throw res;
  }

  return parseInt(match[1], 10);
}

function parseLiferayResponse(res) {
  var body = res.body;

  var exception = body && body.exception || '';

  if (!exception) {
    return res;
  }

  if (!res.ok) {
    throw res;
  }

  res.exception = exception;

  res.setStatusProperties(500);

  var found = invoker.knownErrors.some(function (el) {
    if (el.test(exception)) {
      el.act(res)
      return true;
    }
  });

  throw res;
}<commit_msg>Fixed order of exception checks<commit_after>var errors = require('./errors');
var request = require('./request');
var package = require('../package');
var btoa = require('btoa');

module.exports = invoker;

var toString = Object.prototype.toString;

function invoker(portalURL, path, auth, payload) {
  var req = request.post(portalURL + path);

  req.parse && req.parse(request.parse[ 'application/json' ]);
  req.set({"Accept-Encoding" : ""});

  if (arguments.length === 3) {
    payload = auth;
    auth = null;
  }

  if (auth != null) {
    req.set('Authorization', 'Basic ' + btoa(auth.login + ':' + auth.password));
  }

  req.set({ 'User-Agent': 'liferay-core-connector v' + package.version });

  req.redirects(0);

  req.type('json').send(payload);

  return req;
}

invoker.knownErrors = [];

function knownError(test, act) {
  var re = test;
  var exc = act;
  var status = act;

  if (toString.call(test) === '[object RegExp]') {
    test = function (input) {
      return re.test(input);
    };
  }

  if (typeof act === 'number') {
    act = function (res) {
      res.setStatusProperties(el.status);
    };
  }
  else if (act.prototype instanceof Error) {
    act = function (res) {
      throw new exc(res.exception);
    };
  }

  invoker.knownErrors.push({
    test: test,
    act: act
  });
}

knownError(/no *such/i, errors.NotFound);
knownError(/principal *exception/i, errors.PrincipalException);
knownError(/please *sign/i, errors.Unauthorized);
knownError(/authenticated *access/i, errors.Unauthorized);

invoker.parseLiferayResponse = parseLiferayResponse;
invoker.retrieveBuildNumber = retrieveBuildNumber;
invoker.getBody = getBody;

function getBody(res) {
  return res.body;
}

function retrieveBuildNumber(res) {
  var header = res.headers[ 'liferay-portal' ];

  if (!header) {
    throw res;
  }

  var match = header.match(/Build\s+(\d+)/i);

  if (!match) {
    throw res;
  }

  return parseInt(match[1], 10);
}

function parseLiferayResponse(res) {
  var body = res.body;

  var exception = body && body.exception || '';

  if (res.unauthorized) {
    throw new errors.Unauthorized();
  }
  else if (!res.ok) {
    throw res;
  }

  if (!exception) {
    return res;
  }

  res.exception = exception;

  res.setStatusProperties(500);

  var found = invoker.knownErrors.some(function (el) {
    if (el.test(exception)) {
      el.act(res)
      return true;
    }
  });

  throw res;
}
{'fraction_non_alphanumeric': 0.16126997476871321, 'fraction_numerical': 0.00336417157275021, 'mean_word_length': 3.3363719234275298, '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': '4688815', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1596, 'n_words': 354}