From 693e6c2481f7db5da07276032e93a270cba84593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Sat, 8 May 2021 22:40:25 +0200 Subject: [PATCH] Update: Better error message for unavailable plexPlayer --- dist/plex-meets-homeassistant.js | 32 +++++++++++++++++++++----------- src/modules/PlayController.ts | 32 ++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index cafb176..e76f07d 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18806,19 +18806,29 @@ class PlayController { this.playViaPlexPlayer = async (movieID) => { const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID); const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${this.plex.ip}&commandID=1&containerKey=/playQueues/${playQueueID}?window=100%26own=1&key=/library/metadata/${playQueueSelectedMetadataItemID}&machineIdentifier=${await this.plex.getServerID()}&offset=0&port=${this.plex.port}&token=${this.plex.token}&type=video&protocol=${this.plex.protocol}`; - const plexResponse = await axios({ - method: 'post', - url, - headers: { - 'X-Plex-Target-Client-Identifier': this.entity.plexPlayer, - 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' + try { + const plexResponse = await axios({ + method: 'post', + url, + headers: { + 'X-Plex-Target-Client-Identifier': this.entity.plexPlayer, + 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' + } + }); + if (plexResponse.status !== 200) { + throw Error('Error while asking plex to play a movie - server request error.'); + } + if (!lodash.includes(plexResponse.data, 'status="OK"')) { + throw Error('Error while asking plex to play a movie - target device not available.'); } - }); - if (plexResponse.status !== 200) { - throw Error('Error while asking plex to play a movie - server request error.'); } - if (!lodash.includes(plexResponse.data, 'status="OK"')) { - throw Error('Error while asking plex to play a movie - target device not available.'); + catch (err) { + if (lodash.includes(err.message, '404')) { + throw Error('Defined plexPlayer is currently not available for playback.'); + } + else { + throw err; + } } }; this.playViaKodi = async (data, type) => { diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index 03a350b..285be00 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -127,19 +127,27 @@ class PlayController { }&commandID=1&containerKey=/playQueues/${playQueueID}?window=100%26own=1&key=/library/metadata/${playQueueSelectedMetadataItemID}&machineIdentifier=${await this.plex.getServerID()}&offset=0&port=${ this.plex.port }&token=${this.plex.token}&type=video&protocol=${this.plex.protocol}`; - const plexResponse = await axios({ - method: 'post', - url, - headers: { - 'X-Plex-Target-Client-Identifier': this.entity.plexPlayer, - 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' + try { + const plexResponse = await axios({ + method: 'post', + url, + headers: { + 'X-Plex-Target-Client-Identifier': this.entity.plexPlayer, + 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' + } + }); + if (plexResponse.status !== 200) { + throw Error('Error while asking plex to play a movie - server request error.'); + } + if (!_.includes(plexResponse.data, 'status="OK"')) { + throw Error('Error while asking plex to play a movie - target device not available.'); + } + } catch (err) { + if (_.includes(err.message, '404')) { + throw Error('Defined plexPlayer is currently not available for playback.'); + } else { + throw err; } - }); - if (plexResponse.status !== 200) { - throw Error('Error while asking plex to play a movie - server request error.'); - } - if (!_.includes(plexResponse.data, 'status="OK"')) { - throw Error('Error while asking plex to play a movie - target device not available.'); } };