Updated Somnia API exceptions to use the new API error handling.

This commit is contained in:
2024-10-18 17:58:27 +02:00
parent facd5bc9c0
commit fbee23c022
3 changed files with 58 additions and 23 deletions
+20 -5
View File
@@ -2,6 +2,8 @@
namespace App\Controller;
use App\ApiErrorException;
use App\DTO\ApiErrorMessage;
use App\Repository\FareTableRepository;
use App\Repository\RoomAvailabilityRepository;
use App\Repository\RoomCategoryRepository;
@@ -53,11 +55,16 @@ class SearchRoomsController extends AbstractController
#[MapRequestPayload()] SearchAvailabilityPeriodRequest $searchRequest
): Response {
if (\is_null($searchRequest)) {
throw new HttpException('Invalid Payload', 400);
throw new ApiErrorException(
new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT)
);
}
if ($searchRequest->startDate >= $searchRequest->endDate) {
throw new HttpException(400, "startDate cant be before endDate");
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
'startDate cant be before endDate',
);
}
$rs = $repoAvailability->getAvailabilityForPeriod($searchRequest->startDate, $searchRequest->endDate);
@@ -118,16 +125,24 @@ class SearchRoomsController extends AbstractController
#[MapRequestPayload()] SearchPricesForCategoryRequest $searchRequest
): Response {
if (\is_null($searchRequest)) {
throw new \Exception('Invalid Payload', 400);
throw new ApiErrorException(
new ApiErrorMessage(400, ApiErrorMessage::TYPE_INVALID_REQUEST_BODY_FORMAT)
);
}
$roomCategory = $repoCat->find($searchRequest->roomCategory);
if ($roomCategory === null) {
throw new \Exception('Invalid Category', 400);
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
'Invalid RoomCategory: ' . $searchRequest->roomCategory,
);
}
if ($searchRequest->startDate >= $searchRequest->endDate) {
throw new \Exception('Invalid time interval', 400);
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
'startDate cant be before endDate',
);
}
$fareList = $priceService->getFinalPricesForSearch($roomCategory, $searchRequest->startDate, $searchRequest->endDate);