From 4fc71c2f6f7105ff2ff1430e88ec402d434939a5 Mon Sep 17 00:00:00 2001 From: Tectual Date: Sat, 17 Oct 2015 16:25:49 +0330 Subject: [PATCH] report crashes --- .gitignore | 29 +++++++++++++++ build/main.js | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ gulpfile.coffee | 12 +++++++ package.json | 37 +++++++++++++++++++ src/main.coffee | 58 ++++++++++++++++++++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 .gitignore create mode 100644 build/main.js create mode 100644 gulpfile.coffee create mode 100644 package.json create mode 100644 src/main.coffee diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..126688f --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# Logs +logs +*.log +*.swp + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# Commenting this out is preferred by some people, see +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- +node_modules + +# Users Environment Variables +.lock-wscript diff --git a/build/main.js b/build/main.js new file mode 100644 index 0000000..ea372bf --- /dev/null +++ b/build/main.js @@ -0,0 +1,95 @@ +(function() { + var GoodRollbar, Hoek, SafeJson, Squeeze, _, defaults, rollbar; + + rollbar = require('rollbar'); + + Hoek = require('hoek'); + + Squeeze = require('good-squeeze').Squeeze; + + SafeJson = require('good-squeeze').SafeJson; + + _ = require('lodash'); + + defaults = { + accessToken: null, + exitOnUncaughtException: true, + rollbar: { + environment: process.env.NODE_ENV || 'development', + endpoint: "https://api.rollbar.com/api/1/" + } + }; + + GoodRollbar = (function() { + function GoodRollbar(events, config) { + if (config == null) { + config = {}; + } + this._settings = Hoek.applyToDefaults(defaults, config); + this._streams = { + squeeze: Squeeze(events), + stringify: SafeJson(null, { + separator: '\n' + }) + }; + } + + GoodRollbar.prototype.init = function(stream, emitter, callback) { + console.log('######## INIT GOOD ROLLBAR'); + rollbar.handleUncaughtExceptions(this._settings.accessToken, { + exitOnUncaughtException: this._settings.exitOnUncaughtException + }); + rollbar.init(this._settings.accessToken, this._settings.rollbar); + this._streams.squeeze.on('data', (function(_this) { + return function(data) { + var error, request, url; + error = null; + _.each(data.log, function(log) { + if (_.includes(log.tags, 'error')) { + return error = log; + } + }); + if (error != null) { + request = null; + _.each(data.log, function(log) { + if (_.includes(log.tags, 'received')) { + return request = log; + } + }); + url = data.instance.split('://'); + request = { + headers: { + host: url[1] + }, + url: request.data.url, + method: data.method, + protocol: url[0], + route: { + path: data.path + } + }; + return rollbar.handleError(error.data, request); + } + }; + })(this)); + stream.pipe(this._streams.squeeze); + return callback(); + }; + + return GoodRollbar; + + })(); + + module.exports.attributes = { + pkg: require('../package.json') + }; + + module.exports = function(events, config) { + if (config == null) { + config = {}; + } + console.log('######## NEW GOOD ROLLBAR'); + return new GoodRollbar(events, config); + }; + +}).call(this); diff --git a/gulpfile.coffee b/gulpfile.coffee new file mode 100644 index 0000000..1fc5d38 --- /dev/null +++ b/gulpfile.coffee @@ -0,0 +1,12 @@ +gulp = require 'gulp' +coffee = require 'gulp-coffee' + +gulp.task 'build', -> + + src = 'src/**' + dest = 'build' + + gulp.src( src ) + .pipe( coffee() ) + .pipe( gulp.dest dest ) + diff --git a/package.json b/package.json new file mode 100644 index 0000000..7fde3ee --- /dev/null +++ b/package.json @@ -0,0 +1,37 @@ +{ + "name": "good-hapi-rollbar", + "version": "0.1.0", + "description": "Good Rollbar reporter for Hapi.js", + "main": "build/main", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@github.com:tectual/good-hapi-rollbar.git" + }, + "keywords": [ + "good", + "hapi", + "rollbar", + "error", + "reporting" + ], + "author": "Arash Kay (http://tectual.com.au/)", + "license": "ISC", + "bugs": { + "url": "https://github.com/tectual/good-hapi-rollbar/issues" + }, + "homepage": "https://github.com/tectual/good-hapi-rollbar", + "dependencies": { + "good-squeeze": "^2.1.0", + "hoek": "^2.16.3", + "lodash": "^3.10.1", + "rollbar": "^0.5.8" + }, + "devDependencies": { + "coffee-script": "^1.10.0", + "gulp": "^3.9.0", + "gulp-coffee": "^2.3.1" + } +} diff --git a/src/main.coffee b/src/main.coffee new file mode 100644 index 0000000..9bcbfe1 --- /dev/null +++ b/src/main.coffee @@ -0,0 +1,58 @@ +rollbar = require 'rollbar' +Hoek = require 'hoek' +Squeeze = require('good-squeeze').Squeeze +SafeJson = require('good-squeeze').SafeJson +_ = require 'lodash' + +defaults = { + accessToken: null + exitOnUncaughtException: true + rollbar: { + environment: process.env.NODE_ENV || 'development' + endpoint: "https://api.rollbar.com/api/1/" + } +} + +class GoodRollbar + + constructor: (events, config={}) -> + @_settings = Hoek.applyToDefaults defaults, config + @_streams= + squeeze: Squeeze events + stringify: SafeJson(null, { separator: '\n' }) + + init: (stream, emitter, callback) -> + console.log '######## INIT GOOD ROLLBAR' + rollbar.handleUncaughtExceptions @_settings.accessToken, { exitOnUncaughtException: @_settings.exitOnUncaughtException } + + rollbar.init @_settings.accessToken, @_settings.rollbar + + @_streams.squeeze.on 'data', (data) => + error = null + _.each data.log, (log) -> + error = log if _.includes log.tags, 'error' + if error? + request = null + _.each data.log, (log) -> + request = log if _.includes log.tags, 'received' + url = data.instance.split('://') + request = + headers: + host: url[1] + url: request.data.url + method: data.method + protocol: url[0] + route: + path: data.path + rollbar.handleError(error.data, request) + + stream.pipe(@_streams.squeeze) + + callback() + +module.exports.attributes = + pkg: require('../package.json') + +module.exports = (events, config={}) -> + console.log '######## NEW GOOD ROLLBAR' + new GoodRollbar events, config