<commit_before>var env = require('../../env');
var log = require('../../log')();

module.exports = {
  ok: function okResponse(data) {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    res.status(200);

    if (!data) {
      if (res.locals.record) {
        data = res.locals.record;
      } else {
        data = {};
      }
    } else {
      if (!res.locals.record) {
        res.locals.record = data;
      }
    }

    we.hooks.trigger('we:before:send:okResponse', {
      req: req,
      res: res,
      data: data
    }, function() {
      if (!res.locals.responseType || res.locals.responseType == 'html') {
        return res.view(data);
      }

      var responseData = we.responses.format(res.locals.responseType, data, req, res, we);
      return res.send(responseData);
    });
  },
  created: function createdResponse(data) {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    res.status(201);

    if (!data) {
      if (res.locals.record) {
        data = res.locals.record;
      } else {
        data = {};
      }
    } else {
      if (!res.locals.record) {
        res.locals.record = data;
      }
    }

    return we.hooks.trigger('we:before:send:createdResponse', {
      req: req,
      res: res,
      data: data
    }, function() {
      if (!res.locals.responseType || res.locals.responseType == 'html') {
        return res.view(data);
      }

      var responseData = we.responses.format(res.locals.responseType, data, req, res, we);
      return res.send(responseData);
    });
  },
  deleted: function deletedResponse() {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    res.status(204);

    we.hooks.trigger('we:before:send:deletedResponse', {
      req: req,
      res: res
    }, function() {
      if (!res.locals.responseType || res.locals.responseType == 'html') {
        return res.view();
      }
      res.send();
    });
  },
  view: function viewResponse(data) {
    var req = this.req;
    var res = this.res;
    // resolve and render the template
    res.renderPage(req, res, data);
  },
  forbidden: function forbiddenResponse(data) {
    var res = this.res;

    if (!data) data = {};

    res.status(403);

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      res.locals.template = '403';
      return res.view(data);
    }

    if (!res.locals.model) {
      return res.send({
        messages: res.locals.messages
      });
    }

    var response = {};
    response[res.locals.model] = data;
    response.meta = res.locals.metadata;

    // set messages
    response.messages = res.locals.messages;

    return res.send(response);
  },
  notFound: function notFoundResponse(data) {
    var res = this.res;

    if (!data) data = {};

    if (env == 'dev') console.trace('404');

    res.status(404);
    res.locals.template = '404';

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      return res.view(data);
    }

    data.messages = res.locals.messages;
    return res.send(data);
  },
  serverError: function serverErrorResponse(data) {
    var res = this.res;

    res.status(500);
    res.locals.template = '500';

    if (data && env != 'prod') log.error(data);

    if (!data) data = {};

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      return res.view(data);
    }

    // set messages
    data.messages = res.locals.messages;
    return res.send(data);
  },
  badRequest: function badResponse(data) {
    var res = this.res;

    res.status(400);

    if (!data) {
      data = {};
    } else if (typeof data == 'string') {
      res.addMessage('warning', data);
    }

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      res.locals.template = '400';
      return res.view(data);
    }

    if (!res.locals.model) {
      // set messages
      data.messages = res.locals.messages;
      return res.send(data);
    }

    var response = {};
    response[res.locals.model] = data;
    response.meta = res.locals.metadata;
    // set messages
    response.messages = res.locals.messages;

    return res.send(response);

  },
  queryError: function queryError(err) {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    we.log.verbose('Query error:', err);

    if (err.name === 'SequelizeValidationError') {
      res.locals.validationError = {};
      if (res.locals.responseType === 'html') {
        err.errors.forEach(function (err) {
          res.locals.validationError[err.path] = {
            field: err.path, rule: err.type,
            message: req.__(err.message)
          }
        });
      } else {
        err.errors.forEach(function (err) {
          res.addMessage('error', err.message, {
            field: err.path, rule: err.type
          });
        });
      }
    }

    res.status(400);

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      return res.view();
    }

    if (!res.locals.model) {
      // set messages
      err.messages = res.locals.messages;
      return res.send(err);
    }

    var response = {};
    response[res.locals.model] = err;
    response.meta = res.locals.metadata;
    // set messages
    response.messages = res.locals.messages;

    return res.send();
  }
};<commit_msg>update field erros to suport multiple validation errors<commit_after>var env = require('../../env');
var log = require('../../log')();

module.exports = {
  ok: function okResponse(data) {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    res.status(200);

    if (!data) {
      if (res.locals.record) {
        data = res.locals.record;
      } else {
        data = {};
      }
    } else {
      if (!res.locals.record) {
        res.locals.record = data;
      }
    }

    we.hooks.trigger('we:before:send:okResponse', {
      req: req,
      res: res,
      data: data
    }, function() {
      if (!res.locals.responseType || res.locals.responseType == 'html') {
        return res.view(data);
      }

      var responseData = we.responses.format(res.locals.responseType, data, req, res, we);
      return res.send(responseData);
    });
  },
  created: function createdResponse(data) {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    res.status(201);

    if (!data) {
      if (res.locals.record) {
        data = res.locals.record;
      } else {
        data = {};
      }
    } else {
      if (!res.locals.record) {
        res.locals.record = data;
      }
    }

    return we.hooks.trigger('we:before:send:createdResponse', {
      req: req,
      res: res,
      data: data
    }, function() {
      if (!res.locals.responseType || res.locals.responseType == 'html') {
        return res.view(data);
      }

      var responseData = we.responses.format(res.locals.responseType, data, req, res, we);
      return res.send(responseData);
    });
  },
  deleted: function deletedResponse() {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    res.status(204);

    we.hooks.trigger('we:before:send:deletedResponse', {
      req: req,
      res: res
    }, function() {
      if (!res.locals.responseType || res.locals.responseType == 'html') {
        return res.view();
      }
      res.send();
    });
  },
  view: function viewResponse(data) {
    var req = this.req;
    var res = this.res;
    // resolve and render the template
    res.renderPage(req, res, data);
  },
  forbidden: function forbiddenResponse(data) {
    var res = this.res;

    if (!data) data = {};

    res.status(403);

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      res.locals.template = '403';
      return res.view(data);
    }

    if (!res.locals.model) {
      return res.send({
        messages: res.locals.messages
      });
    }

    var response = {};
    response[res.locals.model] = data;
    response.meta = res.locals.metadata;

    // set messages
    response.messages = res.locals.messages;

    return res.send(response);
  },
  notFound: function notFoundResponse(data) {
    var res = this.res;

    if (!data) data = {};

    if (env == 'dev') console.trace('404');

    res.status(404);
    res.locals.template = '404';

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      return res.view(data);
    }

    data.messages = res.locals.messages;
    return res.send(data);
  },
  serverError: function serverErrorResponse(data) {
    var res = this.res;

    res.status(500);
    res.locals.template = '500';

    if (data && env != 'prod') log.error(data);

    if (!data) data = {};

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      return res.view(data);
    }

    // set messages
    data.messages = res.locals.messages;
    return res.send(data);
  },
  badRequest: function badResponse(data) {
    var res = this.res;

    res.status(400);

    if (!data) {
      data = {};
    } else if (typeof data == 'string') {
      res.addMessage('warning', data);
    }

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      res.locals.template = '400';
      return res.view(data);
    }

    if (!res.locals.model) {
      // set messages
      data.messages = res.locals.messages;
      return res.send(data);
    }

    var response = {};
    response[res.locals.model] = data;
    response.meta = res.locals.metadata;
    // set messages
    response.messages = res.locals.messages;

    return res.send(response);

  },
  queryError: function queryError(err) {
    var res = this.res;
    var req = this.req;
    var we = req.getWe();

    we.log.verbose('Query error:', err);

    if (err.name === 'SequelizeValidationError') {
      res.locals.validationError = {};
      if (res.locals.responseType === 'html') {
        err.errors.forEach(function (err) {
          if (!res.locals.validationError[err.path])
            res.locals.validationError[err.path] = [];

          res.locals.validationError[err.path].push({
            field: err.path, rule: err.type,
            message: req.__(err.message)
          });
        });
      } else {
        err.errors.forEach(function (err) {
          res.addMessage('error', err.message, {
            field: err.path, rule: err.type
          });
        });
      }
    }

    res.status(400);

    if (!res.locals.responseType || res.locals.responseType == 'html') {
      return res.view();
    }

    if (!res.locals.model) {
      // set messages
      err.messages = res.locals.messages;
      return res.send(err);
    }

    var response = {};
    response[res.locals.model] = err;
    response.meta = res.locals.metadata;
    // set messages
    response.messages = res.locals.messages;

    return res.send();
  }
};
{'fraction_non_alphanumeric': 0.15778801843317972, 'fraction_numerical': 0.007188940092165899, 'mean_word_length': 2.160792309933003, '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': '2861336', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3517, 'n_words': 714}