| 
									
										
										
										
											2021-09-29 10:47:10 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-09-29 12:25:06 +02:00
										 |  |  |  * Programmatic sentry middleware. We do not want to expose it in the plugin | 
					
						
							| 
									
										
										
										
											2021-09-29 10:47:10 +02:00
										 |  |  |  * @param {{ strapi: import('@strapi/strapi').Strapi }} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | module.exports = ({ strapi }) => { | 
					
						
							| 
									
										
										
										
											2021-10-13 10:55:46 +02:00
										 |  |  |   const sentryService = strapi.plugin('sentry').service('sentry'); | 
					
						
							|  |  |  |   sentryService.init(); | 
					
						
							|  |  |  |   const sentry = sentryService.getInstance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!sentry) { | 
					
						
							|  |  |  |     // initialization failed
 | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-09-29 10:47:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   strapi.server.use(async (ctx, next) => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await next(); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2022-01-01 16:45:23 +01:00
										 |  |  |       sentryService.sendError(error, (scope, sentryInstance) => { | 
					
						
							| 
									
										
										
										
											2021-09-29 10:47:10 +02:00
										 |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2022-06-09 22:37:33 +02:00
										 |  |  |         scope.setTag('transaction', `${ctx.method} ${ctx._matchedRoute}`); | 
					
						
							| 
									
										
										
										
											2021-09-29 10:47:10 +02:00
										 |  |  |         // Manually add Strapi version
 | 
					
						
							|  |  |  |         scope.setTag('strapi_version', strapi.config.info.strapi); | 
					
						
							|  |  |  |         scope.setTag('method', ctx.method); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       throw error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; |