Refine exception handling for booking controller

Signed-off-by: Anna Larch <anna@nextcloud.com>
This commit is contained in:
Anna Larch 2023-02-01 12:08:27 +01:00
parent 7182d463ec
commit fc01bfd9b5
2 changed files with 27 additions and 3 deletions

View File

@ -42,6 +42,7 @@ use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\IConfig;
use OCP\IRequest;
use OCP\Mail\IMailer;
use Psr\Log\LoggerInterface;
@ -67,6 +68,7 @@ class BookingController extends Controller {
/** @var IMailer */
private $mailer;
private IConfig $systemConfig;
public function __construct(string $appName,
IRequest $request,
@ -76,7 +78,8 @@ class BookingController extends Controller {
AppointmentConfigService $appointmentConfigService,
URLGenerator $urlGenerator,
LoggerInterface $logger,
IMailer $mailer) {
IMailer $mailer,
IConfig $systemConfig) {
parent::__construct($appName, $request);
$this->bookingService = $bookingService;
@ -86,6 +89,7 @@ class BookingController extends Controller {
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->mailer = $mailer;
$this->systemConfig = $systemConfig;
}
/**
@ -190,7 +194,17 @@ class BookingController extends Controller {
return JsonResponse::fail(null, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch (ServiceException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return JsonResponse::errorFromThrowable($e, $e->getHttpCode() ?? Http::STATUS_INTERNAL_SERVER_ERROR);
if ($this->systemConfig->getSystemValue('debug', false)) {
return JsonResponse::errorFromThrowable($e, $e->getHttpCode() ?? Http::STATUS_INTERNAL_SERVER_ERROR,
['debug' => true,]
);
}
return JsonResponse::error(
'Server error',
$e->getHttpCode() ?? Http::STATUS_INTERNAL_SERVER_ERROR
);
}
return JsonResponse::success($booking);

View File

@ -39,6 +39,7 @@ use OCP\AppFramework\Services\IInitialState;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Calendar\ICalendarQuery;
use OCP\Contacts\IManager;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
use OCP\IUser;
@ -87,6 +88,9 @@ class BookingControllerTest extends TestCase {
/** @var IMailer|MockObject */
private $mailer;
/** @var IConfig|MockObject */
private $systemConfig;
protected function setUp():void {
parent::setUp();
@ -103,6 +107,7 @@ class BookingControllerTest extends TestCase {
$this->urlGenerator = $this->createMock(URLGenerator::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->mailer = $this->createMock(IMailer::class);
$this->systemConfig = $this->createMock(IConfig::class);
$this->controller = new BookingController(
$this->appName,
$this->request,
@ -112,7 +117,8 @@ class BookingControllerTest extends TestCase {
$this->apptService,
$this->urlGenerator,
$this->logger,
$this->mailer
$this->mailer,
$this->systemConfig,
);
}
@ -254,6 +260,10 @@ class BookingControllerTest extends TestCase {
->method('book')
->with($config, 1, 1, 'Europe/Berlin', 'Test', $email, 'Test')
->willThrowException(new ServiceException());
$this->systemConfig->expects(self::once())
->method('getSystemValue')
->with('debug')
->willReturn(false);
$this->controller->bookSlot(1, 1, 1, 'Test', $email, 'Test', 'Europe/Berlin');
}