Skip to content

Commit

Permalink
fix: aggregator errors handler (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 committed Sep 28, 2023
1 parent 8a53b3e commit 1ec122c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/nervous-horses-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/api': patch
'@hyperdx/app': patch
---

fix: aggregator errors handler status code
22 changes: 15 additions & 7 deletions packages/api/src/aggregator-app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import compression from 'compression';
import express from 'express';
import { serializeError } from 'serialize-error';

import * as clickhouse from './clickhouse';
import logger, { expressLogger } from './utils/logger';
import routers from './routers/aggregator';
import { appErrorHandler } from './middleware/error';
import { BaseError, StatusCode } from './utils/errors';
import { mongooseConnection } from './models';

import type { Request, Response, NextFunction } from 'express';
Expand All @@ -18,23 +19,23 @@ const healthCheckMiddleware = async (
) => {
if (mongooseConnection.readyState !== 1) {
logger.error('MongoDB is down!');
return res.status(500).send('MongoDB is down!');
return res.status(StatusCode.INTERNAL_SERVER).send('MongoDB is down!');
}

try {
await clickhouse.healthCheck();
} catch (e) {
logger.error('Clickhouse is down!');
return res.status(500).send('Clickhouse is down!');
return res.status(StatusCode.INTERNAL_SERVER).send('Clickhouse is down!');
}
next();
};

app.disable('x-powered-by');
app.use(compression());
app.use(express.json({ limit: '64mb' }));
app.use(express.text({ limit: '64mb' }));
app.use(express.urlencoded({ extended: false, limit: '64mb' }));
app.use(express.json({ limit: '144mb' })); // WARNING: should be greater than the upstream batch size limit
app.use(express.text({ limit: '144mb' }));
app.use(express.urlencoded({ extended: false, limit: '144mb' }));

app.use(expressLogger);

Expand All @@ -45,6 +46,13 @@ app.use('/', healthCheckMiddleware, routers.rootRouter);
// ---------------------------------------------------------

// error handling
app.use(appErrorHandler);
app.use((err: BaseError, _: Request, res: Response, next: NextFunction) => {

Check warning on line 49 in packages/api/src/aggregator-app.ts

View workflow job for this annotation

GitHub Actions / lint

'next' is defined but never used
logger.error({
location: 'appErrorHandler',
error: serializeError(err),
});
// WARNING: should always return 500 so the ingestor will queue logs
res.status(StatusCode.INTERNAL_SERVER).send('Something broke!');
});

export default app;

0 comments on commit 1ec122c

Please sign in to comment.