<commit_before>'use strict';

var path = require('path');
var _ = require('lodash');

var RestifizerScope = require('./scope');
var DataService = require('./data-service');
var utils = require('./utils');

var requireOptions = utils.requireOptions;
var CommonController = utils.CommonController;

class Controller extends DataService {

  constructor(options) {

    super(options);

    var requiredOptions = ['dataSource', 'path'];
    requireOptions(this, requiredOptions);

    this.actions = this.actions || [];

    // init

    this.defaultOptions = _.merge(
      {
        enabled: true,
        method: 'get',
        priority: 1
      },
      this.actions.default || {}
    );

    this.actions.select = _.merge(
      {},
      this.actions.default,
      {
        method: ['get', 'head'],
        handler: 'select',
        name: 'select',
        path: ''
      },
      this.actions.select
    );

    this.actions.count = _.merge(
      {},
      this.actions.select,
      {
        method: ['get', 'head'],
        handler: 'count',
        name: 'count',
        path: 'count'
      },
      this.actions.count
    );

    this.actions.selectOne = _.merge(
      {},
      this.actions.select,
      {
        method: ['get', 'head'],
        handler: 'selectOne',
        name: 'selectOne',
        path: ':' + this.idField
      },
      this.actions.selectOne
    );

    this.actions.insert = _.merge(
      {},
      this.actions.default,
      {
        method: 'post',
        handler: 'insert',
        name: 'insert',
        path: ''
      },
      this.actions.insert
    );

    this.actions.update = _.merge(
      {},
      this.actions.insert,
      {
        method: 'patch',
        handler: 'update',
        name: 'update',
        path: ':' + this.idField
      },
      this.actions.update
    );

    this.actions.replace = _.merge(
      {},
      this.actions.update,
      {
        method: 'put',
        handler: 'replace',
        name: 'replace',
        path: ':' + this.idField
      },
      this.actions.replace
    );

    this.actions.delete = _.merge(
      {},
      this.actions.update,
      {
        method: 'delete',
        handler: 'delete',
        name: 'delete',
        path: ':' + this.idField
      },
      this.actions.delete
    );

    delete this.actions.default;

    this.actions = _.mapValues(this.actions, (action, actionKey) => {
      return this.normalizeAction(action, actionKey);
    });

    _.each(options.plugins, (pluginData) => {
      pluginData.plugin(this, pluginData.options);
    });
  }

  bind(app) {

    if (typeof(this.path) === 'string') {
      this.path = [this.path];
    }
    _.forEach(this.path, (path) => {
      _.forEach(_.sortBy(this.actions, 'priority'), (action) => {
        try {
          if (action.enabled) {
            if (typeof(action.method) === 'string') {
              action.method = [action.method];
            }
            _.forEach(action.method, (method) => {
              app[method](path + '/' + action.path,
                this.getAuth(action),
                (req, res, next) => {
                  var scope = new RestifizerScope(action, this.contextFactory);
                  scope.owner = this;
                  scope.req = req;
                  scope.res = res;
                  scope.model = {};

                  action.handler
                    .call(action, scope)
                    .then((data) => {
                      if (typeof data != 'undefined') {
                        action.setResData(data, scope);
                      }
                    })
                    .catch((err) => {
                      action.setResError(err, scope);
                    })
                    .then(() => {
                      action.sendResult(scope);
                      next();
                    })
                  ;
                }
              );
            });
          }<commit_msg>fix catching error in handlers<commit_after>'use strict';

var path = require('path');
var _ = require('lodash');
var Bb = require('bluebird');

var RestifizerScope = require('./scope');
var DataService = require('./data-service');
var utils = require('./utils');

var requireOptions = utils.requireOptions;
var CommonController = utils.CommonController;

class Controller extends DataService {

  constructor(options) {

    super(options);

    var requiredOptions = ['dataSource', 'path'];
    requireOptions(this, requiredOptions);

    this.actions = this.actions || [];

    // init

    this.defaultOptions = _.merge(
      {
        enabled: true,
        method: 'get',
        priority: 1
      },
      this.actions.default || {}
    );

    this.actions.select = _.merge(
      {},
      this.actions.default,
      {
        method: ['get', 'head'],
        handler: 'select',
        name: 'select',
        path: ''
      },
      this.actions.select
    );

    this.actions.count = _.merge(
      {},
      this.actions.select,
      {
        method: ['get', 'head'],
        handler: 'count',
        name: 'count',
        path: 'count'
      },
      this.actions.count
    );

    this.actions.selectOne = _.merge(
      {},
      this.actions.select,
      {
        method: ['get', 'head'],
        handler: 'selectOne',
        name: 'selectOne',
        path: ':' + this.idField
      },
      this.actions.selectOne
    );

    this.actions.insert = _.merge(
      {},
      this.actions.default,
      {
        method: 'post',
        handler: 'insert',
        name: 'insert',
        path: ''
      },
      this.actions.insert
    );

    this.actions.update = _.merge(
      {},
      this.actions.insert,
      {
        method: 'patch',
        handler: 'update',
        name: 'update',
        path: ':' + this.idField
      },
      this.actions.update
    );

    this.actions.replace = _.merge(
      {},
      this.actions.update,
      {
        method: 'put',
        handler: 'replace',
        name: 'replace',
        path: ':' + this.idField
      },
      this.actions.replace
    );

    this.actions.delete = _.merge(
      {},
      this.actions.update,
      {
        method: 'delete',
        handler: 'delete',
        name: 'delete',
        path: ':' + this.idField
      },
      this.actions.delete
    );

    delete this.actions.default;

    this.actions = _.mapValues(this.actions, (action, actionKey) => {
      return this.normalizeAction(action, actionKey);
    });

    _.each(options.plugins, (pluginData) => {
      pluginData.plugin(this, pluginData.options);
    });
  }

  bind(app) {

    if (typeof(this.path) === 'string') {
      this.path = [this.path];
    }
    _.forEach(this.path, (path) => {
      _.forEach(_.sortBy(this.actions, 'priority'), (action) => {
        try {
          if (action.enabled) {
            if (typeof(action.method) === 'string') {
              action.method = [action.method];
            }
            _.forEach(action.method, (method) => {
              app[method](path + '/' + action.path,
                this.getAuth(action),
                (req, res, next) => {
                  var scope = new RestifizerScope(action, this.contextFactory);
                  scope.owner = this;
                  scope.req = req;
                  scope.res = res;
                  scope.model = {};

                  Bb
                    .try(() => {
                      return action.handler.call(action, scope);
                    })
                    .then((data) => {
                      if (typeof data != 'undefined') {
                        action.setResData(data, scope);
                      }
                    })
                    .catch((err) => {
                      action.setResError(err, scope);
                    })
                    .then(() => {
                      action.sendResult(scope);
                      next();
                    })
                  ;
                }
              );
            });
          }
{'fraction_non_alphanumeric': 0.14731865123802415, 'fraction_numerical': 0.00024884907303720296, 'mean_word_length': 1.4116411641164117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '719132', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2194, 'n_words': 407}