2020-12-14 00:59:12 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Sentry = require('@sentry/node');
|
|
|
|
const defaultSettings = require('../config/settings.json');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
isReady: false,
|
|
|
|
_instance: null,
|
2020-12-14 16:15:35 +01:00
|
|
|
settings: {},
|
2020-12-14 00:59:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Sentry service
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
// Make sure there isn't a Sentry instance already running
|
|
|
|
if (this._instance != null) {
|
|
|
|
strapi.log.warn('Sentry has already been initialized');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve user settings and merge them with the default ones
|
2020-12-14 16:15:35 +01:00
|
|
|
this.settings = {
|
2020-12-14 00:59:12 +01:00
|
|
|
...defaultSettings,
|
|
|
|
...strapi.plugins.sentry.config,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-12-14 16:15:35 +01:00
|
|
|
// Don't init Sentry if no DSN was provided
|
|
|
|
if (this.settings.dsn) {
|
2020-12-14 00:59:12 +01:00
|
|
|
Sentry.init({
|
2020-12-14 16:15:35 +01:00
|
|
|
dsn: this.settings.dsn,
|
2020-12-14 00:59:12 +01:00
|
|
|
environment: strapi.config.environment,
|
|
|
|
});
|
|
|
|
// Store the successfully initialized Sentry instance
|
|
|
|
this._instance = Sentry;
|
|
|
|
this.isReady = true;
|
2020-12-14 16:15:35 +01:00
|
|
|
} else {
|
|
|
|
strapi.log.info('strapi-plugin-sentry is disabled because no Sentry DSN was provided');
|
2020-12-14 00:59:12 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
strapi.log.warn('Could not set up Sentry, make sure you entered a valid DSN');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose Sentry instance through a getter
|
|
|
|
* @returns {Sentry}
|
|
|
|
*/
|
|
|
|
getInstance() {
|
|
|
|
return this._instance;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to [configure an instance of Sentry's scope]{@link https://docs.sentry.io/platforms/node/enriching-events/scopes/#configuring-the-scope}
|
|
|
|
* @callback configureScope
|
|
|
|
* @param {Sentry.scope} scope
|
|
|
|
* @param {Sentry=} instance An initialized Sentry instance
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Higher level method to send exception events to Sentry
|
|
|
|
* @param {Error} error An error object
|
|
|
|
* @param {configureScope=} configureScope
|
|
|
|
*/
|
|
|
|
sendError(error, configureScope) {
|
|
|
|
// Make sure Sentry is ready
|
|
|
|
if (!this.isReady) {
|
|
|
|
strapi.log.warn("Sentry wasn't properly initialized, cannot send event");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._instance.withScope(scope => {
|
|
|
|
// Configure the Sentry scope using the provided callback
|
2020-12-14 16:15:35 +01:00
|
|
|
if (this.settings.sendMetadata) {
|
|
|
|
configureScope(scope, this._instance);
|
|
|
|
}
|
2020-12-14 00:59:12 +01:00
|
|
|
// Actually send the Error to Sentry
|
|
|
|
this._instance.captureException(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
strapi.log.info('An error was sent to Sentry');
|
|
|
|
},
|
|
|
|
};
|