Skip to content

Commit

Permalink
Improve error handling on tsoa
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Nov 28, 2023
1 parent 6940deb commit 841786f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/engine/paima-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export * from './cde-config/loading.js';
export * from './cde-config/validation.js';
export * from './cde-config/utils.js';
export * from './types.js';
export { registerDocs } from './server.js';
export { registerDocs, registerValidationErrorHandler } from './server.js';
export { TimeoutError } from './utils.js';

process.on('SIGINT', () => {
Expand Down
29 changes: 27 additions & 2 deletions packages/engine/paima-runtime/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import cors from 'cors';
import type { Express } from 'express';
import type { Express, Response as ExResponse, Request as ExRequest, NextFunction } from 'express';
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import { basicControllerJson } from '@paima/rest';
import { merge, isErrorResult } from 'openapi-merge';
import { doLog, logError } from '@paima/utils';
import path from 'path';
import { ValidateError } from 'tsoa';

const server: Express = express();
const bodyParser = express.json();
Expand Down Expand Up @@ -37,6 +38,30 @@ function getOpenApiJson(userStateMachineApi: object | undefined): object {
}
}

function registerValidationErrorHandler(): void {
server.use(function errorHandler(
err: unknown,
req: ExRequest,
res: ExResponse,
next: NextFunction
): ExResponse | void {
if (err instanceof ValidateError) {
console.warn(`Caught Validation Error for ${req.path}:`, err.fields);
return res.status(422).json({
message: 'Validation Failed',
details: err?.fields,
});
}
if (err instanceof Error) {
return res.status(500).json({
message: 'Internal Server Error',
});
}

next();
});
}

function registerDocs(userStateMachineApi: object | undefined): void {
const swaggerUiPath = path.resolve(__dirname) + '/swagger-ui';
const swaggerServer = [
Expand All @@ -50,4 +75,4 @@ function registerDocs(userStateMachineApi: object | undefined): void {
const openApi = getOpenApiJson(userStateMachineApi);
server.use('/docs', swaggerServer, swaggerUi.setup(openApi, { explorer: false }));
}
export { server, startServer, registerDocs };
export { server, startServer, registerDocs, registerValidationErrorHandler };
3 changes: 2 additions & 1 deletion packages/engine/paima-standalone/src/utils/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FunnelFactory } from '@paima/funnel';
import paimaRuntime, { registerDocs } from '@paima/runtime';
import paimaRuntime, { registerDocs, registerValidationErrorHandler } from '@paima/runtime';
import { ENV, doLog } from '@paima/utils';
import { exec } from 'child_process';
import { createInterface } from 'readline';
Expand Down Expand Up @@ -115,6 +115,7 @@ export const runPaimaEngine = async (): Promise<void> => {
RegisterRoutes(server);
});
registerDocs(importOpenApiJson());
registerValidationErrorHandler();

void engine.run(ENV.STOP_BLOCKHEIGHT, ENV.SERVER_ONLY_MODE);
} else {
Expand Down

0 comments on commit 841786f

Please sign in to comment.