From bec1eb6cebefaacdd0045bc581b881eb63971d3a Mon Sep 17 00:00:00 2001 From: Kris Date: Mon, 15 Feb 2021 21:43:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AC=20Catch=20DB-Rest's=20502=20Bad=20?= =?UTF-8?q?Gateway=20and=20show=20a=20user=20friendly=20error=20message=20?= =?UTF-8?q?(#205)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Catch DB-Rest's 502 Bad Gateway and show a user friendly warning * Language --- .../Controllers/API/TransportController.php | 20 ++++--- .../FrontendTransportController.php | 30 +++++++---- app/Http/Controllers/HafasController.php | 52 +++++++++++++++---- app/Http/Controllers/TransportController.php | 8 +++ resources/lang/de/messages.php | 10 ++-- resources/lang/en/messages.php | 10 ++-- 6 files changed, 94 insertions(+), 36 deletions(-) diff --git a/app/Http/Controllers/API/TransportController.php b/app/Http/Controllers/API/TransportController.php index 8d37c770d..e14108290 100644 --- a/app/Http/Controllers/API/TransportController.php +++ b/app/Http/Controllers/API/TransportController.php @@ -38,11 +38,15 @@ public function TrainStationboard(Request $request): JsonResponse { $validated = $validator->validate(); - $trainStationboardResponse = TransportBackend::TrainStationboard( - $validated['station'], - isset($validated['when']) ? Carbon::parse($validated['when']) : null, - $validated['travelType'] ?? null - ); + try { + $trainStationboardResponse = TransportBackend::TrainStationboard( + $validated['station'], + isset($validated['when']) ? Carbon::parse($validated['when']) : null, + $validated['travelType'] ?? null + ); + } catch (HafasException $exception) { + return $this->sendError(400, $exception->getMessage()); + } if ($trainStationboardResponse === false) { return $this->sendError(400, __('controller.transport.no-name-given')); } @@ -108,7 +112,11 @@ public function TrainCheckin(Request $request) { if ($hafasTrip == null && strlen($request->input('lineName')) == 0) { return $this->sendError('Please specify the trip with lineName.', 400); } else if ($hafasTrip == null) { - $hafasTrip = HafasController::getHafasTrip($request->input('tripID'), $request->input('lineName')); + try { + $hafasTrip = HafasController::getHafasTrip($request->input('tripID'), $request->input('lineName')); + } catch (HafasException $exception) { + return $this->sendError($exception->getMessage(), 400); + } } try { diff --git a/app/Http/Controllers/FrontendTransportController.php b/app/Http/Controllers/FrontendTransportController.php index aaffd49ba..e59760101 100644 --- a/app/Http/Controllers/FrontendTransportController.php +++ b/app/Http/Controllers/FrontendTransportController.php @@ -42,11 +42,15 @@ public function TrainStationboard(Request $request): Renderable|RedirectResponse $when = isset($validated['when']) ? Carbon::parse($validated['when']) : null; - $TrainStationboardResponse = TransportBackend::TrainStationboard( - $validated['station'], - $when, - $validated['travelType'] ?? null - ); + try { + $TrainStationboardResponse = TransportBackend::TrainStationboard( + $validated['station'], + $when, + $validated['travelType'] ?? null + ); + } catch (HafasException $exception) { + return back()->with('error', $exception->getMessage()); + } if ($TrainStationboardResponse === false) { return redirect()->back()->with('error', __('controller.transport.no-name-given')); } @@ -90,12 +94,16 @@ public function TrainTrip(Request $request): Renderable|RedirectResponse { 'departure' => ['required', 'date'] ]); - $TrainTripResponse = TransportBackend::TrainTrip( - $request->tripID, - $request->lineName, - $request->start, - Carbon::parse($request->departure) - ); + try { + $TrainTripResponse = TransportBackend::TrainTrip( + $request->tripID, + $request->lineName, + $request->start, + Carbon::parse($request->departure) + ); + } catch (HafasException $exception) { + return back()->with('error', $exception->getMessage()); + } if ($TrainTripResponse === null) { return redirect()->back()->with('error', __('controller.transport.not-in-stopovers')); } diff --git a/app/Http/Controllers/HafasController.php b/app/Http/Controllers/HafasController.php index 7c6b39e02..42cc79ccc 100644 --- a/app/Http/Controllers/HafasController.php +++ b/app/Http/Controllers/HafasController.php @@ -103,6 +103,23 @@ public static function getNearbyStations(float $latitude, float $longitude, int } } + /** + * @param TrainStation $station + * @param Carbon $when + * @param int $duration + * @param bool $nationalExpress + * @param bool $national + * @param bool $regionalExp + * @param bool $regional + * @param bool $suburban + * @param bool $bus + * @param bool $ferry + * @param bool $subway + * @param bool $tram + * @param bool $taxi + * @return Collection + * @throws HafasException + */ public static function getDepartures( TrainStation $station, Carbon $when, @@ -150,6 +167,12 @@ public static function getDepartures( } } + /** + * @param string $tripID + * @param string $lineName + * @return HafasTrip + * @throws HafasException + */ public static function getHafasTrip(string $tripID, string $lineName): HafasTrip { $trip = HafasTrip::where('trip_id', $tripID)->first(); if ($trip !== null) { @@ -159,16 +182,27 @@ public static function getHafasTrip(string $tripID, string $lineName): HafasTrip return self::fetchHafasTrip($tripID, $lineName); } + /** + * @param string $tripID + * @param string $lineName + * @return HafasTrip + * @throws HafasException + */ public static function fetchHafasTrip(string $tripID, string $lineName): HafasTrip { - $tripClient = new Client(['base_uri' => config('trwl.db_rest')]); - $tripResponse = $tripClient->get("trips/$tripID", [ - 'query' => [ - 'lineName' => $lineName, - 'polyline' => 'true', - 'stopovers' => 'true' - ] - ]); - $tripJson = json_decode($tripResponse->getBody()->getContents()); + $tripClient = new Client(['base_uri' => config('trwl.db_rest')]); + try { + $tripResponse = $tripClient->get("trips/$tripID", [ + 'query' => [ + 'lineName' => $lineName, + 'polyline' => 'true', + 'stopovers' => 'true' + ] + ]); + } catch (GuzzleException) { + //sometimes DB-Rest gives 502 Bad Request + throw new HafasException(__('messages.exception.generalHafas')); + } + $tripJson = json_decode($tripResponse->getBody()->getContents()); $origin = self::parseHafasStopObject($tripJson->origin); $destination = self::parseHafasStopObject($tripJson->destination); diff --git a/app/Http/Controllers/TransportController.php b/app/Http/Controllers/TransportController.php index 016fd7ac7..d436e7392 100644 --- a/app/Http/Controllers/TransportController.php +++ b/app/Http/Controllers/TransportController.php @@ -56,6 +56,13 @@ public static function BusAutocomplete($station) { return $array; } + /** + * @param $stationName + * @param Carbon|null $when + * @param null $travelType + * @return bool|array|null + * @throws HafasException + */ public static function TrainStationboard($stationName, Carbon $when = null, $travelType = null): bool|array|null { if (empty($stationName)) { return false; @@ -175,6 +182,7 @@ public static function sortByWhenOrScheduledWhen(array $departuresList): array { * @param $start * @param Carbon|null $departure * @return array|null + * @throws HafasException */ public static function TrainTrip(string $tripId, string $lineName, $start, Carbon $departure = null): ?array { diff --git a/resources/lang/de/messages.php b/resources/lang/de/messages.php index 99fb5af3c..a48342273 100644 --- a/resources/lang/de/messages.php +++ b/resources/lang/de/messages.php @@ -1,10 +1,10 @@ "Wir nutzen Cookies für unser Login-System.", + "cookie-notice" => "Wir nutzen Cookies für unser Login-System.", "cookie-notice-button" => "Okay", - "cookie-notice-learn" => "Mehr erfahren", - "exception" => [ - "general" => "Es ist ein Fehler aufgetreten. Bitte probiere es erneut.", - "generalHafas" => "Es gab ein Problem mit der HAFAS Schnittstelle.", + "cookie-notice-learn" => "Mehr erfahren", + "exception" => [ + "general" => "Es ist ein Fehler aufgetreten. Bitte probiere es erneut.", + "generalHafas" => "Es gab ein Problem mit der Schnittstelle zu den Fahrplandaten. Bitte probiere es erneut.", ] ]; diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 38870cad8..6cede79f5 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -1,10 +1,10 @@ "We use cookies for our login-system.", + "cookie-notice" => "We use cookies for our login-system.", "cookie-notice-button" => "Okay", - "cookie-notice-learn" => "Learn more", - "exception" => [ - "general" => "An unknown error occurred. Please try again.", - 'generalHafas' => "There has been a Problem with the HAFAS interface." + "cookie-notice-learn" => "Learn more", + "exception" => [ + "general" => "An unknown error occurred. Please try again.", + 'generalHafas' => "There was a problem with the interface to the timetable data. Please try again." ] ];