<commit_before>var express = require('express');
var router = express.Router();
var Promise = require('bluebird');

var BeamServer = require('./model/BeamServer');
var beamServer = new BeamServer();

var ex = require('./exceptions');

var Promise = require('bluebird');

var fs = require('fs');
fs = Promise.promisifyAll(fs);
router.get('/', function(req, res) {
    fs.readFileAsync(__dirname + '/../demo.html')
    .then(function(data) {
        res.end(data);
    });
});

router.post('/rpc/registration', function(req, res) {
    // get post information
    var url = req.body.url;
    var name = req.body.name;
    if (url === undefined || name === undefined) {
        throw new ex.InvalidRequestError();
    }

    // connect and assume everything is good
    try {
        beamServer.connect(name, url);
    } catch (e) {
        console.log('Error while connecting to the host');
        console.log(e.toString());
        var code = parseInt(e.message);

        throw e;
    }

    // if connected to RPC ok, send a 201
    res.send(201).end();
});

// returns a list of all rpc servers
router.get('/rpc', function(req, res) {
    res.send(200, Object.keys(beamServer.clients)).end();
});

router.get('/:name', function(req, res) {
    var name = req.params.name;

    res.set('Access-Control-Allow-Origin', '*');

    // 404 if the client doesn't exist
    var client = beamServer.clients[name];
    if (client === undefined) {
        throw new ex.FuncNotFoundError();
    }

    // send the functions associated with that client
    res.send(200, client.functions).end();
});

router.get('/:name/:function', function(req, res) {
    var name = req.params.name;
    var fn = req.params.function;

    res.set('Access-Control-Allow-Origin', '*');

    // attempt to get the client
    var client = beamServer.clients[name];
    //if (client === undefined) {
    //    throw new ex.FuncNotFoundError();
    //}

    // build up the parameters
    var args = [];
    if (req.query.args !== undefined) {
        // split on comma unless escaped
        args = req.query.args.split(/[^\\],/);
    }

    console.log('-- here are the args --');
    console.log(args);

    client.call(fn, args)
    .then(function(result) {
        res.send(200, result);
    });
});

module.exports = router;<commit_msg>added error handling for missing arguments<commit_after>var express = require('express');
var router = express.Router();
var Promise = require('bluebird');

var BeamServer = require('./model/BeamServer');
var beamServer = new BeamServer();

var ex = require('./exceptions');

var Promise = require('bluebird');

router.post('/rpc/registration', function(req, res) {
    // get post information
    var url = req.body.url;
    var name = req.body.name;
    if (url === undefined || name === undefined) {
        throw new ex.InvalidRequestError();
    }

    // connect and assume everything is good
    try {
        beamServer.connect(name, url);
    } catch (e) {
        console.log('Error while connecting to the host');
        console.log(e.toString());
        var code = parseInt(e.message);

        throw e;
    }

    // if connected to RPC ok, send a 201
    res.send(201).end();
});

// returns a list of all rpc servers
router.get('/rpc', function(req, res) {
    res.send(200, Object.keys(beamServer.clients)).end();
});

router.get('/:name', function(req, res) {
    var name = req.params.name;

    res.set('Access-Control-Allow-Origin', '*');

    // 404 if the client doesn't exist
    var client = beamServer.clients[name];
    if (client === undefined) {
        throw new ex.FuncNotFoundError();
    }

    // send the functions associated with that client
    res.send(200, client.functions).end();
});

router.get('/:name/:function', function(req, res) {
    var name = req.params.name;
    var fn = req.params.function;

    res.set('Access-Control-Allow-Origin', '*');

    // attempt to get the client
    var client = beamServer.clients[name];
    //if (client === undefined) {
    //    throw new ex.FuncNotFoundError();
    //}

    // build up the parameters
    var args = [];
    if (req.query.args !== undefined) {
        // split on comma unless escaped
        args = req.query.args.split(/[^\\],/);
    }

    console.log('-- here are the args --');
    console.log(args);

    client.call(fn, args)
    .then(function(result) {
        res.send(200, result);
    })
    .catch(function(err) {
        console.log('there was a zerorpc error');
        res.status(500).end();
    });
});

module.exports = router;
{'fraction_non_alphanumeric': 0.1655338741504056, 'fraction_numerical': 0.00855075641306731, 'mean_word_length': 2.667202572347267, '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': '4698868', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1377, 'n_words': 363}