Skip to content

Instantly share code, notes, and snippets.

@Zhomart
Created November 16, 2016 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zhomart/0952897efac3bc1bf454edb1db029fd1 to your computer and use it in GitHub Desktop.
Save Zhomart/0952897efac3bc1bf454edb1db029fd1 to your computer and use it in GitHub Desktop.
import request from 'superagent';
import errorStackParser from 'error-stack-parser';
import { merge } from 'object-state-storage';
class Rollbar {
constructor(logger) {
this._logger = logger;
this.setSessionId = this.setSessionId.bind(this);
this.error = this.error.bind(this);
}
setSessionId(sessionId) {
this.sessionId = sessionId;
}
//
// Read more about rollbar api here https://rollbar.com/docs/api/items_post
//
error(exception, state, req, res) {
// assume provided exception is some instance of an Error
const data = this.__payload(exception, state, req, res);
this._logger.log('Error data: ', data);
if (ROLLBAR_ENABLED) { // do not report errors in development mode
// trace
const rollbarReq = request
.post('https://api.rollbar.com/api/1/item/')
.send(data);
rollbarReq.end((err) => {
if (err) {
this._logger.log('Error report wasn\'t sent', err);
}
});
} else {
this._logger.log('Error is not reported. Rollbar is disabled.');
}
}
__payload(exception, state, req, res) {
return {
access_token: ROLLBAR_TOKEN,
data: merge({
environment: APP_ENV,
timestamp: (new Date()).getTime(),
level: 'error',
platform: 'browser',
language: 'javascript',
client: {
browser: global.window.navigator.userAgent,
code_version: VERSION,
source_map_enabled: true,
guess_uncaught_frames: true,
},
request: req,
user_ip: '$remote_ip',
custom: {
state: JSON.stringify(state), // stringify state for easier copy/paste
sessionId: this.sessionId,
},
// fingerprint: null,
// title: null,
notifier: {
name: 'xit-client',
version: VERSION,
},
}, this.__rollbarData(exception, state, req, res)),
};
}
// Generates rollbar data.body
__rollbarData(exception, state, req, res) {
let rollbarData = null;
if (res && res.body && res.body.type && res.body.message) {
rollbarData = {
server: {
code_version: res.headers['xit-version'],
},
body: {
message: {
body: `${res.body.type}: ${res.body.message}`,
error_type: res.body.type,
error_message: res.body.message,
error_data: res.body.data,
status: res.status,
unauthorized: res.unauthorized,
},
},
};
} else {
let frames = [];
try {
frames = errorStackParser.parse(exception).map(frame => ({
_stackFrame: frame,
filename: frame.fileName,
lineno: frame.lineNumber,
colno: frame.columnNumber,
method: frame.functionName,
args: frame.args,
}));
} catch (e) {
this._logger.log(`No stack trace for "${exception.message}"`);
}
rollbarData = {
body: {
trace: {
frames,
exception: {
class: exception.name || '(unknown)',
message: exception.message,
},
},
},
};
}
return rollbarData;
}
}
export default Rollbar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment