<commit_before>"use strict";

var createError = require('http-errors');
var httpAssert = require('http-assert');
var statuses = require('statuses');
var delegate = require('delegates');
var _ = require('lodash');
var sanitize = require('sanitize');
var View = require('./View');

/**
 * Context is your main class.
 * In an `action` and `before` context
 * will be your `this`.  Anything your attach
 * to context will be passed along. Plus context
 * adds a lot of nice conveniences.
 */
class Context {
  constructor(req, res) {
    this.req = this.request = req;
    this.res = this.response = res;
    this.controllerName = null;
    this.actionName = null;

    this.res.serverError = this.serverError.bind(this);

    // Apply Sanitizer
    sanitize.middleware(this, this.res, _.noop);

    View.applyMixin(this);

    // lets add context to res
    this.res.context = this;
  }

  /**
   * Throw
   *
   * Throw an error with `msg` and optional `status`
   * defaulting to 500. Note that these are user-level
   * errors, and the message may be exposed to the client.
   *
   * ```
   *    this.throw(403)
   *    this.throw('name required', 400)
   *    this.throw(400, 'name required')
   *    this.throw('something exploded')
   *    this.throw(new Error('invalid'), 400);
   *    this.throw(400, new Error('invalid'));
   * ```
   *
   * See: https://github.com/jshttp/http-errors
   *
   * @param {String|Number|Error} status, msg or error
   * @param {String|Number|Error} msg, error, or status]
   * @param {Object} [props]
   * @api public
   */
  throw(status, msg, props){
    throw createError.apply(null, arguments);
  }

  /**
   * Similar to .throw(), adds assertion.<commit_msg>emit event on request finished<commit_after>"use strict";

var createError = require('http-errors');
var httpAssert = require('http-assert');
var statuses = require('statuses');
var delegate = require('delegates');
var _ = require('lodash');
var sanitize = require('sanitize');
var View = require('./View');

/**
 * Context is your main class.
 * In an `action` and `before` context
 * will be your `this`.  Anything your attach
 * to context will be passed along. Plus context
 * adds a lot of nice conveniences.
 */
class Context {
  constructor(req, res) {
    this.req = this.request = req;
    this.res = this.response = res;
    this.controllerName = null;
    this.actionName = null;

    this.res.serverError = this.serverError.bind(this);

    // Apply Sanitizer
    sanitize.middleware(this, this.res, _.noop);

    View.applyMixin(this);

    // lets add context to res
    this.res.context = this;

    require('on-finished')(res, function() {
      sand.http.emit('request:finished', this);
    }.bind(this));
  }

  /**
   * Throw
   *
   * Throw an error with `msg` and optional `status`
   * defaulting to 500. Note that these are user-level
   * errors, and the message may be exposed to the client.
   *
   * ```
   *    this.throw(403)
   *    this.throw('name required', 400)
   *    this.throw(400, 'name required')
   *    this.throw('something exploded')
   *    this.throw(new Error('invalid'), 400);
   *    this.throw(400, new Error('invalid'));
   * ```
   *
   * See: https://github.com/jshttp/http-errors
   *
   * @param {String|Number|Error} status, msg or error
   * @param {String|Number|Error} msg, error, or status]
   * @param {Object} [props]
   * @api public
   */
  throw(status, msg, props){
    throw createError.apply(null, arguments);
  }

  /**
   * Similar to .throw(), adds assertion.
{'fraction_non_alphanumeric': 0.14759206798866856, 'fraction_numerical': 0.010198300283286119, 'mean_word_length': 3.0679723502304146, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '6460215', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1104, 'n_words': 318}