Added Sentry / Added comment

This commit is contained in:
2023-05-15 21:58:01 +02:00
parent 6afdb4fcdd
commit 4bfd71f09f
5 changed files with 143 additions and 9 deletions

View File

@ -44,3 +44,4 @@
</script>
</head>
<body>
<!-- The body and html tag need to be left open! -->

View File

@ -1,11 +1,15 @@
import { Signale } from 'signale';
import ConfigHandler from './assets/configHandler';
import express, { Request, Response } from 'express';
import express, { NextFunction, Request, Response } from 'express';
import fileUpload from 'express-fileupload';
import { PrismaClient } from '@prisma/client';
import * as eta from 'eta';
import bodyParser from 'body-parser';
// Sentry
import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import routes from './routes/index.js';
// Get app directory.
@ -31,6 +35,7 @@ export const config = new ConfigHandler(__path + '/config.json', {
db_connection_string: 'mysql://USER:PASSWORD@HOST:3306/DATABASE',
http_listen_address: '127.0.0.1',
http_port: 3000,
sentry_dsn: 'https://ID@sentry.example.com/PROJECTID',
debug: false
});
@ -43,6 +48,27 @@ export const prisma = new PrismaClient({
});
export const app = express();
Sentry.init({
dsn: config.global.sentry_dsn,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app })
],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0
});
// RequestHandler creates a separate execution context using domains, so that every
// transaction/span/breadcrumb is attached to its own Hub instance
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
app.set('x-powered-by', false);
app.engine('html', eta.renderFile);
@ -57,6 +83,18 @@ app.use(express.static(__path + '/static'));
app.use(routes);
// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
// Optional fallthrough error handler
app.use(function onError(err: Error, req: Request, res: Response, next: NextFunction) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
// @ts-ignore
res.end(res.sentry + '\n');
});
app.listen(config.global.http_port, config.global.http_listen_address, () => {
log.web.info(`Listening at http://${config.global.http_listen_address}:${config.global.http_port}`);
});

View File

@ -12,6 +12,9 @@ Router.use('/static', static_routes);
Router.use('/api', api_routes);
Router.use('/', frontend_routes);
Router.get('/debug-sentry', function mainHandler(req, res) {
throw new Error('My first Sentry error!');
});
// Default route.
Router.all('*', function (req, res) {
// TODO: Respond based on content-type (with req.is('application/json'))