<commit_before>var util = require('util');

var http = require('http');
var HTTPTransport = function () {
    // Opbeat currently doesn't support HTTP
    this.defaultPort = 80;
    this.transport = http;
}
HTTPTransport.prototype.send = function (client, message, headers) {
    var options = {
        hostname: client.dsn.host,
        path: client.dsn.path,
        headers: headers,
        method: 'POST',
        port: client.dsn.port || this.defaultPort
    }, req = this.transport.request(options, function (res) {
        res.setEncoding('utf8');
        var body = [];
        res.on('data', function (data) {
            body.push(data);
        });
        res.on('end', function () {
            if(res.statusCode >= 200 && res.statusCode < 300) {
                client.emit('logged', res.headers.location);
            } else {
                var msg = '';
                body = body.join('');
                try {
                    body = JSON.parse(body);
                    msg = body.error_message;
                } catch (e) {}
                client.emit('error', new Error('Opbeat error (' + res.statusCode + '): ' + msg));
            }
        });
    });
    req.on('error', function (err) {
        client.emit('connectionError', err);
    });
    req.end(message);
}
<commit_msg>Graceful fallback if opbeat doesn't return a proper 'error_message'<commit_after>var util = require('util');

var http = require('http');
var HTTPTransport = function () {
    // Opbeat currently doesn't support HTTP
    this.defaultPort = 80;
    this.transport = http;
}
HTTPTransport.prototype.send = function (client, message, headers) {
    var options = {
        hostname: client.dsn.host,
        path: client.dsn.path,
        headers: headers,
        method: 'POST',
        port: client.dsn.port || this.defaultPort
    }, req = this.transport.request(options, function (res) {
        res.setEncoding('utf8');
        var body = [];
        res.on('data', function (data) {
            body.push(data);
        });
        res.on('end', function () {
            if(res.statusCode >= 200 && res.statusCode < 300) {
                client.emit('logged', res.headers.location);
            } else {
                var msg = '';
                body = body.join('');
                try {
                    body = JSON.parse(body);
                    msg = body.error_message || body;
                } catch (e) {}
                client.emit('error', new Error('Opbeat error (' + res.statusCode + '): ' + msg));
            }
        });
    });
    req.on('error', function (err) {
        client.emit('connectionError', err);
    });
    req.end(message);
}

{'fraction_non_alphanumeric': 0.14907063197026021, 'fraction_numerical': 0.006691449814126394, 'mean_word_length': 1.78860103626943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1409594', 'n_tokens_mistral': 783, 'n_tokens_neox': 747, 'n_words': 167}