Skip to content

Commit

Permalink
fix(logger): logger should display the right status code in case of e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
Thenkei committed Oct 15, 2024
1 parent 0ae0dfd commit af73c71
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/agent/src/routes/system/error-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default class ErrorHandling extends BaseRoute {
if (!this.options.isProduction) {
process.nextTick(() => this.debugLogError(context, e));
}

// The error will be used by Logger
throw e;
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/agent/src/routes/system/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default class Logger extends BaseRoute {
await next();
} catch (e) {
error = e;
throw e;
} finally {
let logLevel: LoggerLevel = 'Info';
if (context.response.status >= HttpCode.BadRequest) logLevel = 'Warn';
Expand Down
4 changes: 2 additions & 2 deletions packages/agent/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export enum HttpCode {

export enum RouteType {
// Changing the values of this enum changes the order in which routes are loaded into koa-router.
ErrorHandler = 0,
LoggerHandler = 1,
LoggerHandler = 0,
ErrorHandler = 1,
PublicRoute = 2,
Authentication = 3,
PrivateRoute = 4,
Expand Down
17 changes: 17 additions & 0 deletions packages/agent/test/routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import ScopeInvalidation from '../../src/routes/security/scope-invalidation';
import ErrorHandling from '../../src/routes/system/error-handling';
import HealthCheck from '../../src/routes/system/healthcheck';
import Logger from '../../src/routes/system/logger';
import { RouteType } from '../../src/types';
import * as factories from '../__factories__';

describe('Route index', () => {
Expand Down Expand Up @@ -81,6 +82,22 @@ describe('Route index', () => {
const countCollectionRoutes = COLLECTION_ROUTES_CTOR.length * dataSource.collections.length;
expect(routes.length).toEqual(ROOT_ROUTES_CTOR.length + countCollectionRoutes);
});

test('should order routes correctly', async () => {
const dataSource = setupWithoutRelation();

const routes = makeRoutes(
dataSource,
factories.forestAdminHttpDriverOptions.build(),
factories.forestAdminHttpDriverServices.build(),
);

// with LoggerHandler in first
expect(routes[0].type).toEqual(RouteType.LoggerHandler);

// followed by ErrorHandler
expect(routes[1].type).toEqual(RouteType.ErrorHandler);
});
});

describe('when a data source with an one to many relation', () => {
Expand Down

0 comments on commit af73c71

Please sign in to comment.