<commit_before>var Bluebird = require('bluebird');


module.exports = issueRequest;


function issueRequest (request) {
    return Bluebird.resolve(request)
        .catch(function (err) {
            throw new Error('Error communicating with the webtask cluster: '
                + err.message);
        })
        .then(function (res) {
            if (res.error) throw createResponseError(res);

            // Api compatibility
            res.statusCode = res.status;

            return res;
        });
}

function createResponseError (res) {
    if (res.clientError) return new Error('Invalid request: '
        + res.body && res.body.message
            ? res.body.message
            : res.text);
    if (res.serverError) return new Error('Server error: '
        + res.body && res.body.message
            ? res.body.message
            : res.text);
}<commit_msg>Improve http request error reporting and metadata<commit_after>var Bluebird = require('bluebird');


module.exports = issueRequest;


function issueRequest (request) {
    return Bluebird.resolve(request)
        .catch(normalizeResponseError)
        .then(function (res) {
            // Api compatibility
            res.statusCode = res.status;

            return res;
        });
}

function normalizeResponseError (err) {
    var error;
    var res = err.response;
    
    if (!res) {
        error = new Error(err.message);
    } else if (res.clientError) {
        error = new Error('Invalid request: '
            + res.body && res.body.message
                ? res.body.message
                : res.text);
    } else if (res.serverError) {
        error = new Error('Server error: '
            + res.body && res.body.message
                ? res.body.message
                : res.text);
    }
    
    Object.defineProperty(error, 'stack', { get: function () { return err.stack; }});
    
    error.statusCode = err.status || 500;
    
    return error;
}
{'fraction_non_alphanumeric': 0.1150487930148947, 'fraction_numerical': 0.0015408320493066256, 'mean_word_length': 1.9205397301349325, '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': '2086991', 'n_tokens_mistral': 547, 'n_tokens_neox': 529, 'n_words': 127}