Move sentry middleware to middlewares folder

This commit is contained in:
Alexandre Bodin 2021-02-15 15:45:30 +01:00
parent a850042d90
commit 8d2ab6e1b0
4 changed files with 41 additions and 25 deletions

View File

@ -4,27 +4,4 @@ module.exports = async () => {
// Initialize the Sentry service exposed by this plugin
const { sentry } = strapi.plugins.sentry.services;
sentry.init();
// Create a middleware to intercept API errors
strapi.app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
sentry.sendError(error, (scope, sentryInstance) => {
scope.addEventProcessor(event => {
// Parse Koa context to add error metadata
return sentryInstance.Handlers.parseRequest(event, ctx.request, {
// Don't parse the transaction name, we'll do it manually
transaction: false,
});
});
// Manually add transaction name
scope.setTag('transaction', `${ctx.method} ${ctx.request.url}`);
// Manually add Strapi version
scope.setTag('strapi_version', strapi.config.info.strapi);
scope.setTag('method', ctx.method);
});
throw error;
}
});
};

View File

@ -0,0 +1,5 @@
{
"sentry": {
"enabled": true
}
}

View File

@ -0,0 +1,33 @@
'use strict';
module.exports = strapi => ({
beforeInitialize() {
strapi.config.middleware.load.after.unshift('sentry');
},
initialize() {
const { sentry } = strapi.plugins.sentry.services;
sentry.init();
strapi.app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
sentry.sendError(error, (scope, sentryInstance) => {
scope.addEventProcessor(event => {
// Parse Koa context to add error metadata
return sentryInstance.Handlers.parseRequest(event, ctx.request, {
// Don't parse the transaction name, we'll do it manually
transaction: false,
});
});
// Manually add transaction name
scope.setTag('transaction', `${ctx.method} ${ctx.request.url}`);
// Manually add Strapi version
scope.setTag('strapi_version', strapi.config.info.strapi);
scope.setTag('method', ctx.method);
});
throw error;
}
});
},
});

View File

@ -15,8 +15,7 @@ const createSentryService = () => {
init() {
// Make sure there isn't a Sentry instance already running
if (instance != null) {
strapi.log.warn('Sentry has already been initialized');
return;
return this;
}
// Retrieve user settings and merge them with the default ones
@ -41,6 +40,8 @@ const createSentryService = () => {
} catch (error) {
strapi.log.warn('Could not set up Sentry, make sure you entered a valid DSN');
}
return this;
},
/**