From 5912eb80e15a48255b91e9c2e4a70cf6a89001ee Mon Sep 17 00:00:00 2001 From: drewprice Date: Mon, 22 May 2017 16:57:38 -0400 Subject: [PATCH] Disable airbrake notifications in development --- .env.example | 1 + lib/airbrake.js | 100 +++++++++++++++++++++++++++++------------------- lib/config.js | 55 +++++++++++++++----------- 3 files changed, 94 insertions(+), 62 deletions(-) diff --git a/.env.example b/.env.example index 0aad628..f87246e 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ +AIRBRAKE_ENABLED=false AIRBRAKE_PROJECT_ID=12345 AIRBRAKE_PROJECT_KEY=ABCDEF123456 diff --git a/lib/airbrake.js b/lib/airbrake.js index 5f1a2b1..8e59caf 100644 --- a/lib/airbrake.js +++ b/lib/airbrake.js @@ -1,63 +1,83 @@ 'use babel' -import {app} from 'remote' import commandLog from './command-log' -import {learnCo} from './config' import path from 'path' import post from './post' +import remote from 'remote' import token from './token' -import {parse} from 'stacktrace-parser' +import {learnCo, airbrakeEnabled} from './config' import {name, version} from './application-metadata' +import {parse} from 'stacktrace-parser' -var appVersion = (() => name.includes('atom') ? version : window.LEARN_IDE_VERSION)(); +const fs = remote.require('fs-plus') -function rootDirectory(stack) { - var pkgPath = path.resolve(__dirname, '..'); +const util = { + pkgPath() { + return path.resolve(__dirname, '..') + }, - if (stack.match(pkgPath) === null) { return } + shouldNotify() { + if (airbrakeEnabled != null) { return airbrakeEnabled } - return pkgPath; -} + return fs.isSymbolicLinkSync(this.pkgPath()) + }, -function backtrace(stack='') { - return parse(stack).map((entry) => { - return { - file: entry.file, - line: entry.lineNumber, - column: entry.column, - function: entry.methodName - }; - }); -} + appVersion() { + return name.includes('atom') ? version : window.LEARN_IDE_VERSION + }, + + backtrace(stack='') { + return parse(stack).map((entry) => { + return { + file: entry.file, + line: entry.lineNumber, + column: entry.column, + function: entry.methodName + }; + }); + }, -function payload(err) { - return { - error: { - message: err.message, - type: err.name, - backtrace: backtrace(err.stack) - }, - context: { - environment: name, - os: process.platform, - version: appVersion, - rootDirectory: rootDirectory(err.stack) - }, - additional: { - commands: commandLog.get(), - learn_ide_package_version: version, - occurred_at: Date.now(), - os_detail: navigator.platform, - token: token.get() + rootDirectory(stack) { + if (stack.match(this.pkgPath()) === null) { return } + + return this.pkgPath(); + }, + + payload(err) { + return { + error: { + message: err.message, + type: err.name, + backtrace: this.backtrace(err.stack) + }, + context: { + environment: name, + os: process.platform, + version: this.appVersion(), + rootDirectory: this.rootDirectory(err.stack) + }, + additional: { + commands: commandLog.get(), + core_app_version: version, + package_version: window.LEARN_IDE_VERSION, + occurred_at: Date.now(), + os_detail: navigator.platform, + token: token.get() + } } - }; + } } export default { notify(err) { var url = `${learnCo}/api/v1/learn_ide_airbrake`; - return post(url, payload(err), {'Authorization': `Bearer ${token.get()}`}); + if (!util.shouldNotify()) { + console.warn(`*Airbrake notification will not be sent for "${err.message}"`) + return Promise.resolve() + } + + return post(url, util.payload(err), {'Authorization': `Bearer ${token.get()}`}); } } diff --git a/lib/config.js b/lib/config.js index 76b327b..cb1b3f7 100644 --- a/lib/config.js +++ b/lib/config.js @@ -13,28 +13,39 @@ dotenv.config({ silent: true }); -var defaultConfig = { - host: 'ile.learn.co', - port: 443, - path: 'v2/terminal', - learnCo: 'https://learn.co' +const util = { + defaultConfig: { + host: 'ile.learn.co', + port: 443, + path: 'v2/terminal', + learnCo: 'https://learn.co' + }, + + envConfig() { + return this.clean({ + host: process.env['IDE_WS_HOST'], + port: process.env['IDE_WS_PORT'], + path: process.env['IDE_WS_TERM_PATH'], + learnCo: process.env['IDE_LEARN_CO'], + airbrakeEnabled: this.airbrakeEnabled() + }) + }, + + airbrakeEnabled() { + if (process.env['AIRBRAKE_ENABLED'] === 'true') { return true } + if (process.env['AIRBRAKE_ENABLED'] === 'false') { return false } + }, + + clean(obj) { + var cleanObj = {}; + + Object.keys(obj).forEach((key) => { + if (obj[key] != null) { cleanObj[key] = obj[key] } + }) + + return cleanObj; + } } -var envConfig = { - host: process.env['IDE_WS_HOST'], - port: process.env['IDE_WS_PORT'], - path: process.env['IDE_WS_TERM_PATH'], - learnCo: process.env['IDE_LEARN_CO'] -} - -export default Object.assign({}, defaultConfig, clean(envConfig)) - -function clean(obj) { - var cleanObj = {}; +export default { ...util.defaultConfig, ...util.envConfig() } - Object.keys(obj).forEach((key) => { - if (obj[key] != null) { cleanObj[key] = obj[key] } - }) - - return cleanObj; -}