Skip to content

Commit 08b3ea1

Browse files
committed
fix(appointments): catch actual exception type for invalid timezones
Assisted-by: ClaudeCode:claude-sonnet-5 Signed-off-by: Oleksandr Dzhychko <hey@oleks.dev>
1 parent 74b4577 commit 08b3ea1

4 files changed

Lines changed: 15 additions & 12 deletions

File tree

lib/Controller/BookingController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use OCP\AppFramework\Http\TemplateResponse;
2626
use OCP\AppFramework\Services\IInitialState;
2727
use OCP\AppFramework\Utility\ITimeFactory;
28-
use OCP\DB\Exception;
2928
use OCP\IConfig;
3029
use OCP\IRequest;
3130
use OCP\IURLGenerator;
@@ -94,9 +93,9 @@ public function getBookableSlots(
9493
): JsonResponse {
9594
try {
9695
$tz = new DateTimeZone($timeZone);
97-
} catch (Exception $e) {
96+
} catch (\Exception $e) {
9897
$this->logger->error('Timezone invalid', ['exception' => $e]);
99-
return JsonResponse::fail('Invalid time zone', Http::STATUS_UNPROCESSABLE_ENTITY);
98+
return JsonResponse::fail('Invalid timezone', Http::STATUS_UNPROCESSABLE_ENTITY);
10099
}
101100
// Convert selected date to requesters selected timezone adjusted start and end of day in epoch
102101
$startTimeInTz = (new DateTime($dateSelected, $tz))

lib/Service/Appointments/BookingService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use OCA\Calendar\Exception\ServiceException;
2121
use OCP\AppFramework\Db\DoesNotExistException;
2222
use OCP\AppFramework\Http;
23-
use OCP\DB\Exception;
2423
use OCP\DB\Exception as DbException;
2524
use OCP\EventDispatcher\IEventDispatcher;
2625
use OCP\IUser;
@@ -140,8 +139,8 @@ public function book(AppointmentConfig $config, int $start, int $end, string $ti
140139

141140
try {
142141
$tz = new DateTimeZone($timeZone);
143-
} catch (Exception $e) {
144-
throw new InvalidArgumentException('Could not make sense of the timezone', $e->getCode(), $e);
142+
} catch (\Exception $e) {
143+
throw new InvalidArgumentException('Could not make sense of the timezone', previous: $e);
145144
}
146145

147146
$booking = new Booking();
@@ -156,7 +155,7 @@ public function book(AppointmentConfig $config, int $start, int $end, string $ti
156155
$booking->setTimezone($tz->getName());
157156
try {
158157
$this->bookingMapper->insert($booking);
159-
} catch (Exception $e) {
158+
} catch (DbException $e) {
160159
throw new ServiceException('Could not create booking', 0, $e);
161160
}
162161

tests/php/unit/Controller/BookingControllerTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use ChristophWurst\Nextcloud\Testing\TestCase;
1212
use DateTime;
13-
use Exception;
1413
use InvalidArgumentException;
1514
use OC\URLGenerator;
1615
use OCA\Calendar\Db\AppointmentConfig;
@@ -166,9 +165,15 @@ public function testGetBookableSlotsInvalidTimezone(): void {
166165
->with($apptConfg->getToken());
167166
$this->bookingService->expects(self::never())
168167
->method('getAvailableSlots');
169-
$this->expectException(Exception::class);
168+
$this->logger->expects(self::once())
169+
->method('error')
170+
->with('Timezone invalid');
171+
172+
$response = $this->controller->getBookableSlots($apptConfg->getToken(), $selectedDate, 'Hook/Neverland');
170173

171-
$this->controller->getBookableSlots($apptConfg->getToken(), $selectedDate, 'Hook/Neverland');
174+
self::assertInstanceOf(JsonResponse::class, $response);
175+
self::assertSame(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus());
176+
self::assertSame(['status' => 'fail', 'data' => 'Invalid timezone'], $response->getData());
172177
}
173178

174179
public function testGetBookableSlotsTimezoneIdentical(): void {

tests/php/unit/Service/Appointments/BookingServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use ChristophWurst\Nextcloud\Testing\TestCase;
1212
use DateTimeImmutable;
13-
use Exception;
13+
use InvalidArgumentException;
1414
use OCA\Calendar\Db\AppointmentConfig;
1515
use OCA\Calendar\Db\Booking;
1616
use OCA\Calendar\Db\BookingMapper;
@@ -160,7 +160,7 @@ public function testBookInvalidTimezone(): void {
160160
$this->random->expects(self::never())
161161
->method('generate');
162162

163-
$this->expectException(Exception::class);
163+
$this->expectExceptionObject(new InvalidArgumentException('Could not make sense of the timezone'));
164164
$this->service->book(new AppointmentConfig(), 4054546654, 44545454, 'Nighttime/DAYTIME!', 'Test', 'test@test.com', 'Test');
165165
}
166166

0 commit comments

Comments
 (0)