-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
180 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
module.exports = function Sentry(sails) { | ||
return { | ||
/** | ||
* Default configuration | ||
* | ||
* We do this in a function since the configuration key for | ||
* the hook is itself configurable, so we can't just return | ||
* an object. | ||
*/ | ||
defaults: { | ||
__configKey__: { | ||
dsn: process.env.SENTRY_DSN, | ||
options: { | ||
environment: process.env.NODE_ENV || 'development' | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Initialize the hook | ||
* @param {Function} cb Callback for when we're done initializing | ||
* @return {Function} cb Callback for when we're done initializing | ||
*/ | ||
initialize: function(cb) { | ||
var settings = sails.config[this.configKey]; | ||
|
||
if (!settings.dsn) { | ||
sails.log.verbose('DSN for Sentry is required.'); | ||
return cb(); | ||
} | ||
|
||
const Sentry = require('@sentry/node'); | ||
const { getCurrentHub } = require('@sentry/core'); | ||
const { Severity } = require('@sentry/types'); | ||
const util = require('util'); | ||
|
||
Sentry.init(settings); | ||
|
||
sails.sentry = Sentry; | ||
|
||
const origServerError = sails.hooks.responses.middleware.serverError; | ||
sails.hooks.responses.middleware.serverError = function(err) { | ||
Sentry.captureException(err); | ||
origServerError.bind(this)(...arguments); | ||
} | ||
|
||
for (const level of Object.keys(sails.log)) { | ||
switch (level) { | ||
case 'verbose': | ||
case 'silly': | ||
case 'blank': | ||
case 'ship': | ||
continue; | ||
|
||
case 'debug': | ||
sentryLevel = Severity.Debug; | ||
break; | ||
case 'crit': | ||
case 'error': | ||
sentryLevel = Severity.Error; | ||
break; | ||
case 'info': | ||
sentryLevel = Severity.Info; | ||
break; | ||
case 'warn': | ||
sentryLevel = Severity.Warning; | ||
break; | ||
default: | ||
throw new Error('not sure what to do with ' + level); | ||
} | ||
const origFunction = sails.log[level].bind(sails.log); | ||
sails.log[level] = function() { | ||
getCurrentHub().addBreadcrumb( | ||
{ | ||
category: 'sailslog', | ||
level: sentryLevel, | ||
message: util.format.apply(undefined, arguments), | ||
}, | ||
{ | ||
input: [...arguments], | ||
level, | ||
}, | ||
); | ||
origFunction(...arguments); | ||
} | ||
} | ||
|
||
// handles Bluebird's promises unhandled rejections | ||
process.on('unhandledRejection', function(reason) { | ||
console.error('Unhandled rejection:', reason); | ||
Sentry.captureException(reason); | ||
}); | ||
|
||
// We're done initializing. | ||
return cb(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports.sentry = { | ||
active: !!process.env.SENTRY_DSN, | ||
dsn: process.env.SENTRY_DSN, | ||
options: { | ||
environment: process.env.NODE_ENV || 'development' | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
test/integration/hooks/discordChatBridge/chatBridgeChannel.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const expect = require("chai").expect; | ||
const sinon = require('sinon'); | ||
const ChatBridgeChannel = require("../../../../api/hooks/discordChatBridge/chatBridgeChannel"); | ||
|
||
describe('ChatBridgeChannel', function () { | ||
it('Doesnt try to do anything when non player connects', async function () { | ||
sinon.stub(sails.hooks.sdtdlogs, "getLoggingObject").callsFake(async () => null); | ||
sinon.stub(ChatBridgeChannel.prototype, "start").callsFake(async () => null); | ||
|
||
const textChannel = {}; | ||
const cbc = new ChatBridgeChannel(textChannel, sails.testServer); | ||
await cbc.sendRichConnectedMessageToDiscord({ | ||
player: undefined | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
const chaiAsPromised = require("chai-as-promised"); | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
describe('HELPER sdtd/fill-custom-variables', () => { | ||
it('silently works when undefined data is provided', async () => { | ||
const result = await sails.helpers.sdtd.fillCustomVariables('whoa there ${player.friendlyName}', {} ); | ||
expect(result).to.be.eql('whoa there ${player.friendlyName}'); | ||
}); | ||
it('simple replacements work', async () => { | ||
const result = await sails.helpers.sdtd.fillCustomVariables('whoa there ${friendlyName}', { friendlyName: 'halkeye' } ); | ||
expect(result).to.be.eql('whoa there halkeye'); | ||
}); | ||
it('nested replacements work', async () => { | ||
const result = await sails.helpers.sdtd.fillCustomVariables('whoa there ${player.friendlyName}', { player: { friendlyName: 'halkeye' } } ); | ||
expect(result).to.be.eql('whoa there halkeye'); | ||
}); | ||
}); | ||
|