<commit_before># Functions for getting Controllers and Actions.
env = require('../config/environments/env')
paths = env.paths
routes = require(paths.entryPath + '/routes')
utils = require('./utils')

# given a name, eg "listings#show"
# return function that matches that controller's action (eg the show method of the listings controller)
getAction = (config) ->
  controller = getController(config.controller)
  controller[config.action]

getController = (controllerName) ->
  require(paths.entryPath + "/controllers/#{controllerName}_controller")

# this is the method that renders the request
getHandler = (action) ->
  (req, res, next) ->
    context =
      app: req.rendrApp
      redirectTo: (url) -> res.redirect(url)

    params = req.query || {}
    req.route.keys.forEach (routeKey) ->
      params[routeKey.name] = req.route.params[routeKey.name]

    start = new Date
    action.call context, params, (err, template, data) ->
      utils.stashPerf(req, "data", new Date - start)
      return handleErr(err, req, res) if err

      start = new Date
      res.render template, locals: data, app: req.rendrApp, req: req, (err, html) ->
        if (err)
          console.log("RENDER ERROR", err)
        return handleErr(err, req, res) if err
        res.type('html')
        res.end(html)
        utils.stashPerf(req, "render", new Date - start)

handleErr = (err, req, res) ->
  utils.stashError(req, err)

  if err.statusCode && err.statusCode is 401
    res.redirect('/login')
  else
    if (env.name == 'development')
      throw err
    else
      res.render('error_view', app: req.rendrApp, req: req)

# define routes<commit_msg>when 500 from API, always render error view, but only show error data in development<commit_after># Functions for getting Controllers and Actions.
env = require('../config/environments/env')
paths = env.paths
routes = require(paths.entryPath + '/routes')
utils = require('./utils')

# given a name, eg "listings#show"
# return function that matches that controller's action (eg the show method of the listings controller)
getAction = (config) ->
  controller = getController(config.controller)
  controller[config.action]

getController = (controllerName) ->
  require(paths.entryPath + "/controllers/#{controllerName}_controller")

# this is the method that renders the request
getHandler = (action) ->
  (req, res, next) ->
    context =
      app: req.rendrApp
      redirectTo: (url) -> res.redirect(url)

    params = req.query || {}
    req.route.keys.forEach (routeKey) ->
      params[routeKey.name] = req.route.params[routeKey.name]

    start = new Date
    action.call context, params, (err, template, data) ->
      utils.stashPerf(req, "data", new Date - start)
      return handleErr(err, req, res) if err

      start = new Date
      res.render template, locals: data, app: req.rendrApp, req: req, (err, html) ->
        if (err)
          console.log("RENDER ERROR", err)
        return handleErr(err, req, res) if err
        res.type('html')
        res.end(html)
        utils.stashPerf(req, "render", new Date - start)

handleErr = (err, req, res) ->
  utils.stashError(req, err)

  if err.statusCode && err.statusCode is 401
    res.redirect('/login')
  else
    data =
      app: req.rendrApp
      req: req
    if env.name == 'development'
      data.locals =
        message: err.message
        stack: err.stack
    res.render('error_view', data)

# define routes
{'fraction_non_alphanumeric': 0.12867540029112082, 'fraction_numerical': 0.0026200873362445414, 'mean_word_length': 3.110047846889952, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5768839', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1045, 'n_words': 311}