Ghost/ghost/core/test/unit/shared/sentry.test.js
Hannah Wolfe 62cd52ff98 Improved Sentry server side error reporting
refs: https://github.com/TryGhost/Team/issues/1121
refs: 54574025e0

- The previous change to fall back to a generic error on the server side is resulting in lots of much less useful Sentry reports
- For unexpected errors, change what's sent to Sentry back to context
- This is done by adding a specific code, so we don't have to match on a string that might change
- Also add the error type, id, code & statusCode as tags to the events - these are searchable structured data
- Adding code as a tag also makes it possible to find all errors that showed the generic message
2022-11-23 12:37:24 +00:00

56 lines
2.3 KiB
JavaScript

const assert = require('assert');
const sinon = require('sinon');
const configUtils = require('../../utils/configUtils');
const Sentry = require('@sentry/node');
const {asset} = require('../../../core/frontend/services/theme-engine/handlebars/template');
const fakeDSN = 'https://aaabbbccc000111222333444555667@sentry.io/1234567';
let sentry;
describe('UNIT: sentry', function () {
afterEach(function () {
configUtils.restore();
sinon.restore();
});
describe('No sentry config', function () {
beforeEach(function () {
delete require.cache[require.resolve('../../../core/shared/sentry')];
sentry = require('../../../core/shared/sentry');
});
it('returns expected function signature', function () {
assert.equal(sentry.requestHandler.name, 'expressNoop', 'Should return noop');
assert.equal(sentry.errorHandler.name, 'expressNoop', 'Should return noop');
assert.equal(sentry.captureException.name, 'noop', 'Should return noop');
});
});
describe('With sentry config', function () {
beforeEach(function () {
configUtils.set({sentry: {disabled: false, dsn: fakeDSN}});
delete require.cache[require.resolve('../../../core/shared/sentry')];
sinon.spy(Sentry, 'init');
sentry = require('../../../core/shared/sentry');
});
it('returns expected function signature', function () {
assert.equal(sentry.requestHandler.name, 'sentryRequestMiddleware', 'Should return sentry');
assert.equal(sentry.errorHandler.name, 'sentryErrorMiddleware', 'Should return sentry');
assert.equal(sentry.captureException.name, 'captureException', 'Should return sentry');
});
it('initialises sentry correctly', function () {
const initArgs = Sentry.init.getCall(0).args;
assert.equal(initArgs[0].dsn, fakeDSN, 'shoudl be our fake dsn');
assert.match(initArgs[0].release, /ghost@\d+\.\d+\.\d+/, 'should be a valid version');
assert.equal(initArgs[0].environment, 'testing', 'should be the testing env');
assert.ok(initArgs[0].hasOwnProperty('beforeSend'), 'should have a beforeSend function');
});
});
});