From 0cbebb8b636c1c3fcd61ccf9a32684b925334ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20de=20Wit?= Date: Wed, 26 Sep 2018 05:27:16 +0200 Subject: [PATCH] Avoid errors when a flag image is missing (#716) * Avoid errors when a flag image is missing * Fix images flagging * Fix formatting * Fix try/catch --- .../torrent-details-modal/TorrentPeers.js | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/client/src/javascript/components/modals/torrent-details-modal/TorrentPeers.js b/client/src/javascript/components/modals/torrent-details-modal/TorrentPeers.js index 085ba1b3..633e8ad1 100644 --- a/client/src/javascript/components/modals/torrent-details-modal/TorrentPeers.js +++ b/client/src/javascript/components/modals/torrent-details-modal/TorrentPeers.js @@ -16,12 +16,14 @@ export default class TorrentPeers extends React.Component { }; } + flagImageAsErrored(countryCode) { + let {erroredCountryImages} = this.state; + erroredCountryImages.push(countryCode); + this.setState({erroredCountryImages}); + } + getImageErrorHandlerFn(countryCode) { - return () => { - let {erroredCountryImages} = this.state; - erroredCountryImages.push(countryCode); - this.setState({erroredCountryImages}); - }; + return () => this.flagImageAsErrored(countryCode); } render() { @@ -38,16 +40,24 @@ export default class TorrentPeers extends React.Component { let image = null; if (!erroredCountryImages.includes(countryCode)) { - const flagImageSrc = require(`../../../../images/flags/${countryCode.toLowerCase()}.png`); + let flagImageSrc; + try { + flagImageSrc = require(`../../../../images/flags/${countryCode.toLowerCase()}.png`); + } catch (err) { + this.flagImageAsErrored(countryCode); + flagImageSrc = null; + } - image = ( - {countryCode} - ); + if (flagImageSrc) { + image = ( + {countryCode} + ); + } } peerCountry = (