Skip to content

Commit

Permalink
💬 Catch DB-Rest's 502 Bad Gateway and show a user friendly error mess…
Browse files Browse the repository at this point in the history
…age (#205)

* Catch DB-Rest's 502 Bad Gateway and show a user friendly warning

* Language
  • Loading branch information
MrKrisKrisu authored Feb 15, 2021
1 parent 1a68505 commit bec1eb6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 36 deletions.
20 changes: 14 additions & 6 deletions app/Http/Controllers/API/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 19 additions & 11 deletions app/Http/Controllers/FrontendTransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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'));
}
Expand Down
52 changes: 43 additions & 9 deletions app/Http/Controllers/HafasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down
10 changes: 5 additions & 5 deletions resources/lang/de/messages.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
return [
"cookie-notice" => "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.",
]
];
10 changes: 5 additions & 5 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
return [
"cookie-notice" => "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."
]
];

0 comments on commit bec1eb6

Please sign in to comment.