feat: improved exception handling

This commit is contained in:
Björn Fromme
2025-03-27 09:56:00 +01:00
parent 485d37518f
commit 589787f024
7 changed files with 50 additions and 51 deletions
+12 -14
View File
@@ -46,7 +46,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getPersonalData(string $email, string $password): Notification|PersonalData public function getPersonalData(string $email, string $password): Notification|PersonalData
{ {
@@ -64,7 +63,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function register(RegistrationData $registrationData): Notification public function register(RegistrationData $registrationData): Notification
{ {
@@ -88,7 +86,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function resetPassword(string $email): Notification public function resetPassword(string $email): Notification
{ {
@@ -105,7 +102,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function updatePersonalData( public function updatePersonalData(
string $email, string $email,
@@ -129,7 +125,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function updateNewsletterRegistration(string $email, string $password, PersonalData $personalData): Notification|PersonalData public function updateNewsletterRegistration(string $email, string $password, PersonalData $personalData): Notification|PersonalData
{ {
@@ -152,7 +147,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getBookings(string $email, string $password): Notification|BaseData public function getBookings(string $email, string $password): Notification|BaseData
{ {
@@ -168,6 +162,9 @@ class ApiClient
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data); return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
} }
/**
* @throws ApiClientException
*/
public function getBooking(string $email, string $password, int $id): Notification|Booking public function getBooking(string $email, string $password, int $id): Notification|Booking
{ {
$data = [ $data = [
@@ -185,7 +182,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function updateBooking(BookingData $formData, bool $debug = false): Notification|BookingUpdate public function updateBooking(BookingData $formData, bool $debug = false): Notification|BookingUpdate
{ {
@@ -204,7 +200,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getMutableData(int $id): Notification|BaseData public function getMutableData(int $id): Notification|BaseData
{ {
@@ -221,7 +216,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getAvailabilities(int $id): Notification|BaseData public function getAvailabilities(int $id): Notification|BaseData
{ {
@@ -237,7 +231,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getCrmAttributes(string $email, string $password): Notification|CrmAttributes public function getCrmAttributes(string $email, string $password): Notification|CrmAttributes
{ {
@@ -255,7 +248,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getBaseData(string $type): Notification|BaseData public function getBaseData(string $type): Notification|BaseData
{ {
@@ -270,7 +262,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
public function getDocuments(string $email, string $password, int $id, string $type): mixed public function getDocuments(string $email, string $password, int $id, string $type): mixed
{ {
@@ -289,7 +280,6 @@ class ApiClient
/** /**
* @throws ApiClientException * @throws ApiClientException
* @throws ResponseParserException
*/ */
private function sendRequest(string $type, array $data, bool $debug = false): mixed private function sendRequest(string $type, array $data, bool $debug = false): mixed
{ {
@@ -327,7 +317,15 @@ class ApiClient
$this->dumpXmlToFile('response', $requestId, $xml); $this->dumpXmlToFile('response', $requestId, $xml);
} }
return $this->responseParser->parseXmlString($type, $xml); try {
return $this->responseParser->parseXmlString($type, $xml);
} catch (ResponseParserException $e) {
$this->dumpXmlToFile('response', $requestId, $xml);
}
throw new ApiClientException('Unexpected response received from API', [
'request_id' => $requestId,
]);
} }
private function dumpXmlToFile(string $type, string $requestId, string $body): void private function dumpXmlToFile(string $type, string $requestId, string $body): void
@@ -4,7 +4,6 @@ namespace App\Controller\Booking;
use App\BusProNet\ApiClient; use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\ResponseParserException;
use App\BusProNet\Model\Notification; use App\BusProNet\Model\Notification;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -62,7 +61,7 @@ class DownloadController extends AbstractController
$file = $this $file = $this
->apiClient ->apiClient
->getDocuments($bpnUser->getEmail(), $bpnUser->getPassword(), $id, $type); ->getDocuments($bpnUser->getEmail(), $bpnUser->getPassword(), $id, $type);
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$file = null; $file = null;
} }
+31 -26
View File
@@ -73,7 +73,7 @@ class EditController extends AbstractController
// Fetch mutability information via API // Fetch mutability information via API
try { try {
$mutableData = $this->apiClient->getMutableData($bookingData->travelId); $mutableData = $this->apiClient->getMutableData($bookingData->travelId);
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', 'Reisedaten nicht (mehr) verfügbar'); $this->addFlash('error', 'Reisedaten nicht (mehr) verfügbar');
return $this->redirectToRoute('app_bookings'); return $this->redirectToRoute('app_bookings');
@@ -82,7 +82,7 @@ class EditController extends AbstractController
// Fetch availability information via API // Fetch availability information via API
try { try {
$availabilities = $this->apiClient->getAvailabilities($bookingData->travelId); $availabilities = $this->apiClient->getAvailabilities($bookingData->travelId);
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', 'Reisedaten nicht (mehr) verfügbar'); $this->addFlash('error', 'Reisedaten nicht (mehr) verfügbar');
return $this->redirectToRoute('app_bookings'); return $this->redirectToRoute('app_bookings');
@@ -103,36 +103,41 @@ class EditController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$response = $this->apiClient->updateBooking($formData, true);
$this->logger->info('Initiated booking update', [ $this->logger->info('Initiated booking update', [
'email' => $bpnUser->getEmail(), 'email' => $bpnUser->getEmail(),
'booking_id' => $id, 'booking_id' => $id,
]); ]);
if ($response instanceof Notification) {
if (true === $response->isError()) { try {
$this->addFlash('error', $response->message); $response = $this->apiClient->updateBooking($formData, true);
if ($response instanceof Notification) {
if (true === $response->isError()) {
$this->addFlash('error', $response->message);
} else {
$this->addFlash('info', $response->message);
}
$this->logger->error('Booking update not successful', [
'email' => $bpnUser->getEmail(),
'booking_id' => $id,
'message' => $response->message,
]);
} else { } else {
$this->addFlash('info', $response->message); try {
$this->cache->delete($cacheKey);
} catch (InvalidArgumentException $e) {
}
$this->addFlash('success', 'Buchung erfolgreich aktualisiert');
$this->logger->info('Booking update successful', [
'email' => $bpnUser->getEmail(),
'booking_id' => $id,
]);
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
} }
$this->logger->error('Booking update not successful', [ } catch (ApiClientException $e) {
'email' => $bpnUser->getEmail(), $this->addFlash('error', 'Es ist ein Fehler in der Kommunikation mit dem Buchungssystem aufgetreten');
'booking_id' => $id,
'message' => $response->message,
]);
} else {
try {
$this->cache->delete($cacheKey);
} catch (InvalidArgumentException $e) {
}
$this->addFlash('success', 'Buchung erfolgreich aktualisiert');
$this->logger->info('Booking update successful', [
'email' => $bpnUser->getEmail(),
'booking_id' => $id,
]);
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
} }
} }
+1 -2
View File
@@ -4,7 +4,6 @@ namespace App\Controller\Booking;
use App\BusProNet\ApiClient; use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\ResponseParserException;
use App\BusProNet\Model\BaseData; use App\BusProNet\Model\BaseData;
use App\BusProNet\XmlLoader\TravelLoader; use App\BusProNet\XmlLoader\TravelLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -35,7 +34,7 @@ class IndexController extends AbstractController
try { try {
$bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword()); $bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', 'Buchungen nicht abrufbar'); $this->addFlash('error', 'Buchungen nicht abrufbar');
$bookings = new BaseData([]); $bookings = new BaseData([]);
} }
+3 -4
View File
@@ -4,7 +4,6 @@ namespace App\Controller;
use App\BusProNet\ApiClient; use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\ResponseParserException;
use App\BusProNet\Model\PersonalData; use App\BusProNet\Model\PersonalData;
use App\Form\PersonalDataType; use App\Form\PersonalDataType;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -38,7 +37,7 @@ class PersonalDataController extends AbstractController
$personalData = $this $personalData = $this
->apiClient ->apiClient
->getPersonalData($bpnUser->getEmail(), $bpnUser->getPassword()); ->getPersonalData($bpnUser->getEmail(), $bpnUser->getPassword());
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden'); $this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden');
$personalData = new PersonalData(); $personalData = new PersonalData();
} }
@@ -82,7 +81,7 @@ class PersonalDataController extends AbstractController
$personalData = $this $personalData = $this
->apiClient ->apiClient
->getPersonalData($bpnUser->getEmail(), $bpnUser->getPassword()); ->getPersonalData($bpnUser->getEmail(), $bpnUser->getPassword());
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden'); $this->addFlash('error', 'Deine persönlichen Daten konnten nicht abgerufen werden');
$personalData = new PersonalData(); $personalData = new PersonalData();
} }
@@ -95,7 +94,7 @@ class PersonalDataController extends AbstractController
$this->logger->info('Updated newsletter registration', [ $this->logger->info('Updated newsletter registration', [
'email' => $bpnUser->getEmail(), 'email' => $bpnUser->getEmail(),
]); ]);
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', $e->getMessage()); $this->addFlash('error', $e->getMessage());
} }
+1 -2
View File
@@ -4,7 +4,6 @@ namespace App\Controller;
use App\BusProNet\ApiClient; use App\BusProNet\ApiClient;
use App\BusProNet\Exception\ApiClientException; use App\BusProNet\Exception\ApiClientException;
use App\BusProNet\Exception\ResponseParserException;
use App\Form\Model\RegistrationData; use App\Form\Model\RegistrationData;
use App\Form\RegistrationType; use App\Form\RegistrationType;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -43,7 +42,7 @@ class RegistrationController extends AbstractController
$this->logger->info('Initiated registration', [ $this->logger->info('Initiated registration', [
'email' => $registrationData->email, 'email' => $registrationData->email,
]); ]);
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', $e->getMessage()); $this->addFlash('error', $e->getMessage());
$this->logger->error('Error initiating registration', [ $this->logger->error('Error initiating registration', [
'email' => $registrationData->email, 'email' => $registrationData->email,
+1 -1
View File
@@ -52,7 +52,7 @@ class ResetPasswordController extends AbstractController
$this->logger->info('Initiated password reset', [ $this->logger->info('Initiated password reset', [
'email' => $form->get('email')->getData(), 'email' => $form->get('email')->getData(),
]); ]);
} catch (ApiClientException|ResponseParserException $e) { } catch (ApiClientException $e) {
$this->addFlash('error', $e->getMessage()); $this->addFlash('error', $e->getMessage());
$this->logger->error('Error initiating password reset', [ $this->logger->error('Error initiating password reset', [
'email' => $form->get('email')->getData(), 'email' => $form->get('email')->getData(),