Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #477 from learn-co/improve-airbrake
Browse files Browse the repository at this point in the history
Disable airbrake notifications in development
  • Loading branch information
drewprice authored May 22, 2017
2 parents 42ed749 + 5912eb8 commit cac519a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 62 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
AIRBRAKE_ENABLED=false
AIRBRAKE_PROJECT_ID=12345
AIRBRAKE_PROJECT_KEY=ABCDEF123456
100 changes: 60 additions & 40 deletions lib/airbrake.js
Original file line number Diff line number Diff line change
@@ -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()}`});
}
}

55 changes: 33 additions & 22 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit cac519a

Please sign in to comment.