diff --git a/packages/rest-api/src/actions/find-all.ts b/packages/rest-api/src/actions/find-all.ts index f8567b22..735be468 100644 --- a/packages/rest-api/src/actions/find-all.ts +++ b/packages/rest-api/src/actions/find-all.ts @@ -8,6 +8,14 @@ const makeFindAll = ({ findAll }: FlightUseCase) => { const limit = parseInt(query.limit as string) || 15 const offset = parseInt(query.offset as string) || 0 + if (limit < 0) { + throw new BadRequestException('Limit must be a positive integer') + } + + if (offset < 0) { + throw new BadRequestException('Offset must be a positive integer') + } + const where: FindFlightsWhere = { ...(query.departureIcao && { departureIcao: query.departureIcao.split(',') diff --git a/packages/rest-api/src/exception/bad-request-exception.ts b/packages/rest-api/src/exception/bad-request-exception.ts new file mode 100644 index 00000000..8ba6cba1 --- /dev/null +++ b/packages/rest-api/src/exception/bad-request-exception.ts @@ -0,0 +1,8 @@ +class BadRequestException extends Error { + public constructor(message = 'Bad request') { + super(message) + Object.setPrototypeOf(this, BadRequestException.prototype) + } +} + +export default BadRequestException diff --git a/packages/rest-api/src/express-callback/index.ts b/packages/rest-api/src/express-callback/index.ts index 5e4fbaaa..5661d718 100644 --- a/packages/rest-api/src/express-callback/index.ts +++ b/packages/rest-api/src/express-callback/index.ts @@ -1,5 +1,6 @@ import Request from './request' import NotFoundException from '../exception/not-found-exception' +import BadRequestException from '../exception/bad-request-exception' export type ActionInterface = (req: Request) => Promise @@ -12,6 +13,10 @@ const makeExpressCallback = (action: ActionInterface) => { if (error instanceof NotFoundException) { return res.status(404).json({ status: 404, message: 'Not found' }) } + if (error instanceof BadRequestException) { + return res.status(400).json({ status: 400, message: error.message }) + } + console.error(error) return res .status(500)