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);
}
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;
}
Improve http request error reporting and metadata