Skip to content

Commit

Permalink
feat: sentry error handler improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed May 15, 2024
1 parent b52f236 commit a1dbd76
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
3 changes: 3 additions & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@ export const createApp = (): { app: Express, router: express.Router } => {
}
);

const basePath = process.env.BASE_PATH || '/';
app.use(basePath, router);

return { app, router };
};
35 changes: 3 additions & 32 deletions api/src/graphql/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import type { Server } from 'http';
import { createServer as createHttpServer } from 'http';

import type express from 'express';
import { ApolloServerPlugin, ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import bodyParser from 'body-parser';
import cookierParser from 'cookie-parser';
import cors from 'cors';
import { PORT } from '../../env';
import { schemaWithMiddleware as indexSchema } from './index/schema';
import { schemaWithMiddleware as driveSchema } from './drive/schema';
import { Context, Context as IndexContext, createContext as createIndexContext } from './index/context';
import { Context as DriveContext, createContext as createDriveContext } from './drive/context';
import { getChildLogger } from '../../logger';
import "express-async-errors";
import { errorHandler } from '../../middleware/errors';
import * as Sentry from "@sentry/node";
import { initRedis } from '../../redis';
const logger = getChildLogger({ msgPrefix: 'SERVER' });

function loggerPlugin(): ApolloServerPlugin<Context> {
return {
Expand Down Expand Up @@ -44,24 +36,15 @@ const createApolloDriveServer = (): ApolloServer<DriveContext> => new ApolloServ
plugins: [loggerPlugin()],
});

export const startServer = async (
app: express.Application,
export const addGraphqlRoutes = async (
router: express.Router,
): Promise<Server> => {
logger.debug('Starting server');

if (process.env.REDIS_TLS_URL) {
await initRedis();
}

) => {
const apolloIndex = createApolloIndexServer();
const apolloDrive = createApolloDriveServer();

await apolloIndex.start();
await apolloDrive.start();



router.use(
'/drives',
cors<cors.CorsRequest>(),
Expand All @@ -81,16 +64,4 @@ export const startServer = async (
context: async (params) => createDriveContext(params),
}),
);

const basePath = process.env.BASE_PATH || '/';
app.use(basePath, router);
app.use(errorHandler);
if (process.env.SENTRY_DSN) {
app.use(Sentry.Handlers.errorHandler());
}

const httpServer = createHttpServer(app);
return httpServer.listen({ port: PORT }, () => {
logger.info(`Running on ${PORT}`);
});
};
37 changes: 36 additions & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import type express from 'express';
import * as Sentry from "@sentry/node";
import { createApp } from './app';
import { startServer } from './graphql/server';
import { addGraphqlRoutes } from './graphql/server';
import { getChildLogger } from './logger';
import { closeRedis } from './redis';
import { Server, createServer as createHttpServer } from 'http';
import { PORT } from './env';
import { errorHandler } from './middleware/errors';
import { initRedis } from './redis';
import "express-async-errors";

const logger = getChildLogger({ msgPrefix: 'SERVER' });

const { app, router } = createApp();

async function startServer(
app: express.Application,
router: express.Router,
): Promise<Server> {
await addGraphqlRoutes(router);

logger.debug('Starting server');

if (process.env.REDIS_TLS_URL) {
await initRedis();
}

const basePath = process.env.BASE_PATH || '/';
app.use(basePath, router);

if (process.env.SENTRY_DSN) {
app.use(Sentry.Handlers.errorHandler());
}

app.use(errorHandler);

const httpServer = createHttpServer(app);
return httpServer.listen({ port: PORT }, () => {
logger.info(`Running on ${PORT}`);
});
}


/* istanbul ignore next @preserve */
startServer(app, router)
.then((e) => {
Expand Down
1 change: 1 addition & 0 deletions api/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const transport = process.env.SENTRY_DSN ? {
options: {
sentry: {
dsn: process.env.SENTRY_DSN,
environment: process.env.SENTRY_ENV ?? 'dev',
ignoreErrors: [/Transmitter .+ not found/, /^Failed to fetch strands$/, /Drive with id .+ not found/],
// additional options for sentry
},
Expand Down
9 changes: 3 additions & 6 deletions api/src/middleware/errors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { NextFunction, Request, Response } from "express";
import { getChildLogger } from "../logger";
import { CustomError } from "../errors/CustomError";

const logger = getChildLogger({ msgPrefix: 'Generic Error Handler', });

export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => {
const errorId = (res as { sentry?: string }).sentry;
err.cause = err.cause || 'Unknown';
logger.error({
msg: err.message,
});

res.status(500).send({ errors: err.message });
logger.error(err, err.message, { errorId });
res.status(500).send({ errors: err.message, errorId });
};

0 comments on commit a1dbd76

Please sign in to comment.