1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 58× 2× 2× 58× 1× 1× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 58× 54× 58× 58× 58× 1× 58× 1× 58× 1× 1× 1× 1× 4× 1× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 22× 1× 22× 2× 22× 22× 1× 22× 22× 1× 19× 19× 19× 18× 18× 1× 5× 5× 5× 5× 5× 1× 9× 1× 9× 9× 3× 3× 6× 9× 9× 9× 1× 1× 1× 2× 1× 2× 2× 1× 2× 2× 1× 4× 4× 1× 4× 4× 4× 4× 4× 4× 3× 1× 1× 3× 3× 1× 1× 1× 2× 2× 3× 3× 3× | 'use strict'; var stringify = require('json-stringify-safe'); var parsers = require('./parsers'); var zlib = require('zlib'); var utils = require('./utils'); var uuid = require('node-uuid'); var transports = require('./transports'); var node_util = require('util'); // node_util to avoid confusion with "utils" var events = require('events'); module.exports.version = require('../package.json').version; var extend = Object.assign || function(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var Client = function Client(dsn, options) { if (arguments.length === 0) { // no arguments, use default from environment dsn = process.env.SENTRY_DSN; options = {}; } if (typeof dsn === 'object') { // They must only be passing through options options = dsn; dsn = process.env.SENTRY_DSN; } options = options || {}; this.raw_dsn = dsn; this.dsn = utils.parseDSN(dsn); this.name = options.name || process.env.SENTRY_NAME || require('os').hostname(); this.root = options.root || process.cwd(); this.transport = options.transport || transports[this.dsn.protocol]; this.release = options.release || process.env.SENTRY_RELEASE || ''; this.environment = options.environment || process.env.SENTRY_ENVIRONMENT || ''; this.loggerName = options.logger || ''; this.dataCallback = options.dataCallback; if (this.dsn.protocol === 'https') { // In case we want to provide our own SSL certificates / keys this.ca = options.ca || null; } // enabled if a dsn is set this._enabled = !!this.dsn; var globalContext = this._globalContext = {}; if (options.tags) { globalContext.tags = options.tags; } if (options.extra) { globalContext.extra = options.extra; } this.on('error', function(e) {}); // noop }; node_util.inherits(Client, events.EventEmitter); var proto = Client.prototype; module.exports.Client = Client; proto.getIdent = proto.get_ident = function getIdent(result) { return result.id; }; proto.process = function process(kwargs) { kwargs.modules = utils.getModules(); kwargs.server_name = kwargs.server_name || this.name; Iif (typeof process.version !== 'undefined') { kwargs.extra.node = process.version; } kwargs.environment = kwargs.environment || this.environment; kwargs.extra = extend({}, this._globalContext.extra, kwargs.extra); kwargs.tags = extend({}, this._globalContext.tags, kwargs.tags); kwargs.logger = kwargs.logger || this.loggerName; kwargs.event_id = uuid().replace(/-/g, ''); kwargs.timestamp = new Date().toISOString().split('.')[0]; kwargs.project = this.dsn.project_id; kwargs.platform = 'node'; if (this._globalContext.user) { kwargs.user = this._globalContext.user || kwargs.user; } // Only include release information if it is set if (this.release) { kwargs.release = this.release; } var ident = { id: kwargs.event_id }; if (this.dataCallback) { kwargs = this.dataCallback(kwargs); } // this will happen asynchronously. We don't care about it's response. this._enabled && this.send(kwargs, ident); return ident; }; proto.send = function send(kwargs, ident) { var self = this; var skwargs = stringify(kwargs); zlib.deflate(skwargs, function(err, buff) { var message = buff.toString('base64'), timestamp = new Date().getTime(), headers = { 'X-Sentry-Auth': utils.getAuthHeader(timestamp, self.dsn.public_key, self.dsn.private_key), 'Content-Type': 'application/octet-stream', 'Content-Length': message.length }; self.transport.send(self, message, headers, ident); }); }; proto.captureMessage = function captureMessage(message, kwargs, cb) { Iif (!cb && typeof kwargs === 'function') { cb = kwargs; kwargs = {}; } else { kwargs = kwargs || {}; } var result = this.process(parsers.parseText(message, kwargs)); cb && cb(result); return result; }; proto.captureException = function captureError(err, kwargs, cb) { if (!(err instanceof Error)) { // This handles when someone does: // throw "something awesome"; // We synthesize an Error here so we can extract a (rough) stack trace. err = new Error(err); } var self = this; if (!cb && typeof kwargs === 'function') { cb = kwargs; kwargs = {}; } else { kwargs = kwargs || {}; } parsers.parseError(err, kwargs, function(kw) { var result = self.process(kw); cb && cb(result); }); }; proto.captureError = proto.captureException; // legacy alias proto.captureQuery = function captureQuery(query, engine, kwargs, cb) { if (!cb && typeof kwargs === 'function') { cb = kwargs; kwargs = {}; } else { kwargs = kwargs || {}; } var result = this.process(parsers.parseQuery(query, engine, kwargs)); cb && cb(result); return result; }; /* * Set/clear a user to be sent along with the payload. * * @param {object} user An object representing user data [optional] * @return {Raven} */ proto.setUserContext = function setUserContext(user) { this._globalContext.user = user; }; /* * Merge extra attributes to be sent along with the payload. * * @param {object} extra An object representing extra data [optional] * @return {Raven} */ proto.setExtraContext = function setExtraContext(extra) { this._globalContext.extra = extend({}, this._globalContext.extra, extra); return this; }; /* * Merge tags to be sent along with the payload. * * @param {object} tags An object representing tags [optional] * @return {Raven} */ proto.setTagsContext = function setTagsContext(tags) { this._globalContext.tags = extend({}, this._globalContext.tags, tags); return this; }; proto.patchGlobal = function patchGlobal(cb) { module.exports.patchGlobal(this, cb); return this; }; module.exports.patchGlobal = function patchGlobal(client, cb) { // handle when the first argument is the callback, with no client specified Iif (typeof client === 'function') { cb = client; client = new Client(); // first argument is a string DSN } else Iif (typeof client === 'string') { client = new Client(client); } // at the end, if we still don't have a Client, let's make one! !(client instanceof Client) && (client = new Client()); var called = false; process.on('uncaughtException', function(err) { if (cb) { // bind event listeners only if a callback was supplied var onLogged = function onLogged() { called = false; cb(true, err); }; var onError = function onError() { called = false; cb(false, err); }; if (called) { client.removeListener('logged', onLogged); client.removeListener('error', onError); return cb(false, err); } client.once('logged', onLogged); client.once('error', onError); } called = true; client.captureError(err, function(result) { node_util.log('uncaughtException: ' + client.getIdent(result)); }); }); }; |