<commit_before>var User = require('../models/user'),
    utils = require('../../lib/utils'),
    util = require('util');

module.exports = (function() {
  function index(req, res) {
    if (req.user.admin) {
      User.findAll().complete(function(err, users){
        res.send({ admin: true, users: users });
      });
    } else {
      res.send({ user: req.user });
    }
  }

  function show(req, res) {
    if (req.user.admin || (req.user.id == req.params.user_id)) {
      User.find(req.params.user_id).complete(function(err, user){
        res.send({ user: user });
      });  
    } else {
      res.status(401);
      res.send('401: access denied');
    }
  }

  function create(req, res) {
    if (req.user.admin) {
      req.validate('name', 'isAlphaNumeric');
      req.validate('password', '<PASSWORD>');
      User.createEncrypted({ name: req.body.name, password: <PASSWORD>.body.password },function(err, user){
        if (err) { 
          res.send(err)
        } else {
          res.send(user);
        }
      })
    } else {
      res.send(req.user);
    }
  }
  
  return {
    index: index,
    create: create,
    show: show
  }
})();<commit_msg>[FEATURE] Add http error handling to users controller.<commit_after>var User = require('../models/user'),
    utils = require('../../lib/utils'),
    util = require('util');

module.exports = (function() {
  function index(req, res) {
    if (req.user.admin) {
      User.findAll().complete(function(err, users){
        if (err) {
          res.send(500, { error: err });
        } else {
          res.send({ admin: true, users: users });
        }
      });
    } else {
      res.send({ user: req.user });
    }
  }

  function show(req, res) {
    if (req.user.admin || (req.user.id == req.params.user_id)) {
      User.find(req.params.user_id).complete(function(err, user){
        res.send({ user: user });
      });  
    } else {
      res.status(401, { error: 'Access Denied' });
    }
  }

  function create(req, res) {
    if (req.user.admin) {
      req.validate('name', 'isAlphaNumeric');
      req.validate('password', '<PASSWORD>');
      User.createEncrypted({ name: req.body.name, password: <PASSWORD> },function(err, user){
        if (err) { 
          req.send(500, { error: err });
        } else {
          res.send({ user: user });
        }
      })
    } else {
      res.send({ user: req.user });
    }
  }
  
  return {
    index: index,
    create: create,
    show: show
  }
})();
{'fraction_non_alphanumeric': 0.18596208148446955, 'fraction_numerical': 0.0060508269463493344, 'mean_word_length': 2.119496855345912, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '480872', 'n_tokens_mistral': 836, 'n_tokens_neox': 819, 'n_words': 163}