diff --git a/packages/agent/src/routes/system/error-handling.ts b/packages/agent/src/routes/system/error-handling.ts index 7f074467f..89a0507a1 100644 --- a/packages/agent/src/routes/system/error-handling.ts +++ b/packages/agent/src/routes/system/error-handling.ts @@ -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; } } diff --git a/packages/agent/src/routes/system/logger.ts b/packages/agent/src/routes/system/logger.ts index 742c6eea6..58c57b0a1 100644 --- a/packages/agent/src/routes/system/logger.ts +++ b/packages/agent/src/routes/system/logger.ts @@ -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'; diff --git a/packages/agent/src/types.ts b/packages/agent/src/types.ts index 49520d7e8..1298d1b5d 100644 --- a/packages/agent/src/types.ts +++ b/packages/agent/src/types.ts @@ -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, diff --git a/packages/agent/test/routes/index.test.ts b/packages/agent/test/routes/index.test.ts index ab00d746c..99ab4d7b8 100644 --- a/packages/agent/test/routes/index.test.ts +++ b/packages/agent/test/routes/index.test.ts @@ -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', () => { @@ -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', () => {