From 9062c08f08024909dab71ff4f6e73e0694ebe88a Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Sun, 2 Jul 2023 11:39:36 +0200 Subject: [PATCH] Improve error handling --- public/js/settings-integration-plex.js | 12 ++++++------ src/Api/Plex/PlexApi.php | 2 +- src/Api/Plex/PlexUserClient.php | 1 + src/Factory.php | 5 +---- src/HttpController/PlexController.php | 11 ++++------- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/public/js/settings-integration-plex.js b/public/js/settings-integration-plex.js index d78511d2..46ed63a4 100644 --- a/public/js/settings-integration-plex.js +++ b/public/js/settings-integration-plex.js @@ -196,16 +196,16 @@ async function verifyPlexServerUrl() { }).then(async function (response) { document.getElementById('alertPlexServerUrlLoadingSpinner').classList.add('d-none') - return {'status': response.status, 'message': await response.json()}; - }).then(function (data) { - if (data.status === 200 && data.message === true) { - addAlert('alertPlexServerUrlDiv', 'Connection test successful', 'success') + if (response.status === 400) { + addAlert('alertPlexServerUrlDiv', await response.text(), 'danger') return } - if (data.status === 400) { - addAlert('alertPlexServerUrlDiv', data.message, 'danger') + const data = await response.json() + + if (response.status === 200 && data === true) { + addAlert('alertPlexServerUrlDiv', 'Connection test successful', 'success') return } diff --git a/src/Api/Plex/PlexApi.php b/src/Api/Plex/PlexApi.php index 9515a29e..34bb27e1 100644 --- a/src/Api/Plex/PlexApi.php +++ b/src/Api/Plex/PlexApi.php @@ -207,7 +207,7 @@ public function generatePlexAuthenticationUrl() : string return self::BASE_URL . http_build_query($getParameters); } - public function verifyPlexUrl(PlexUserClientConfiguration $userClientConfiguration) : bool + public function testUserClientConfiguration(PlexUserClientConfiguration $userClientConfiguration) : bool { try { $this->userClient->get($userClientConfiguration); diff --git a/src/Api/Plex/PlexUserClient.php b/src/Api/Plex/PlexUserClient.php index ee107bcb..b0179967 100644 --- a/src/Api/Plex/PlexUserClient.php +++ b/src/Api/Plex/PlexUserClient.php @@ -16,6 +16,7 @@ public function get( self::DEFAULT_HEADERS, ['X-Plex-Token' => (string)$clientConfiguration->getAccessToken()], ), + 'connect_timeout' => 4, ]; $requestUrl = $clientConfiguration->getServerUrl(); diff --git a/src/Factory.php b/src/Factory.php index 29773888..6548875c 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -174,10 +174,7 @@ public static function createExportService(ContainerInterface $container) : Expo public static function createHttpClient() : ClientInterface { - return new GuzzleHttp\Client([ - 'connect_timeout' => 4, - 'timeout' => 4, - ]); + return new GuzzleHttp\Client(['timeout' => 4]); } public static function createImageCacheService(ContainerInterface $container) : ImageCacheService diff --git a/src/HttpController/PlexController.php b/src/HttpController/PlexController.php index 823c82c3..bceb7931 100644 --- a/src/HttpController/PlexController.php +++ b/src/HttpController/PlexController.php @@ -171,22 +171,19 @@ public function verifyPlexServerUrl(Request $request) : Response $plexAccessToken = $this->authenticationService->getCurrentUser()->getPlexAccessToken(); if ($plexAccessToken === null) { - return Response::createBadRequest('Verification failed, plex authentication token missing.'); + return Response::createBadRequest('Plex authentication is missing'); } - $plexServerUrl = Json::decode($request->getBody())['plexServerUrl']; - if (empty($plexServerUrl)) { - return Response::createBadRequest('Url not correctly formatted'); - } + $plexServerUrl = Json::decode($request->getBody())['plexServerUrl'] ?? ''; try { $plexServerUrl = Url::createFromString($plexServerUrl); } catch (InvalidUrl) { - return Response::createBadRequest('Verification failed, url not properly formatted'); + return Response::createBadRequest('Provided server url is not a valid url'); } $userClientConfiguration = PlexUserClientConfiguration::create($plexAccessToken, $plexServerUrl); - return Response::createJson(Json::encode($this->plexApi->verifyPlexUrl($userClientConfiguration))); + return Response::createJson(Json::encode($this->plexApi->testUserClientConfiguration($userClientConfiguration))); } }