mirror of
https://github.com/strapi/strapi.git
synced 2026-01-08 05:04:10 +00:00
Move sentry middleware to middlewares folder
This commit is contained in:
parent
a850042d90
commit
8d2ab6e1b0
@ -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;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
{
|
||||
"sentry": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
33
packages/strapi-plugin-sentry/middlewares/sentry/index.js
Normal file
33
packages/strapi-plugin-sentry/middlewares/sentry/index.js
Normal 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;
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user