Skip to content

Commit

Permalink
feat(rest-api): add bad request response on negative offset and limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed May 22, 2021
1 parent afcdca1 commit fcec730
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/rest-api/src/actions/find-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')
Expand Down
8 changes: 8 additions & 0 deletions packages/rest-api/src/exception/bad-request-exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class BadRequestException extends Error {
public constructor(message = 'Bad request') {
super(message)
Object.setPrototypeOf(this, BadRequestException.prototype)
}
}

export default BadRequestException
5 changes: 5 additions & 0 deletions packages/rest-api/src/express-callback/index.ts
Original file line number Diff line number Diff line change
@@ -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<T> = (req: Request) => Promise<T>

Expand All @@ -12,6 +13,10 @@ const makeExpressCallback = <T>(action: ActionInterface<T>) => {
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)
Expand Down

0 comments on commit fcec730

Please sign in to comment.