From 3fee7bb40a37bee88f7cee6ab5c0240cdffafebf Mon Sep 17 00:00:00 2001 From: Christer Date: Sun, 5 Apr 2020 02:29:39 +0300 Subject: [PATCH] Added checkFetchStatus function and fetch will use it. --- client/class.js | 3 ++- client/events/ready.js | 18 ++++++++++++------ client/funcs/checkFetchStatus.js | 7 +++++++ 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 client/funcs/checkFetchStatus.js diff --git a/client/class.js b/client/class.js index 9dc0fc7..e7091bb 100644 --- a/client/class.js +++ b/client/class.js @@ -17,9 +17,10 @@ module.exports = class extends Client { this.funcs = {}; this.funcs.check = require('./funcs/check.js'); + this.funcs.checkFetchStatus = require('./funcs/checkFetchStatus.js'); + this.funcs.isDev = require('./funcs/isDev.js'); this.funcs.msToTime = require('./funcs/msToTime.js'); this.funcs.statisticsUpdate = require('./funcs/statisticsUpdate.js'); - this.funcs.isDev = require('./funcs/isDev.js'); this.config = require('../config.js'); this.messages = require('./messages.js'); diff --git a/client/events/ready.js b/client/events/ready.js index 81fac5c..1f5726d 100644 --- a/client/events/ready.js +++ b/client/events/ready.js @@ -21,14 +21,20 @@ module.exports = { try { client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') - .then(res => res.json()); - } catch (err) { - console.error(err); + .then(client.funcs.checkFetchStatus) + .then(response => response.json()); + } catch (error) { + console.error(error); } - + setInterval(async () => { - client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') - .then(res => res.json()); + try { + client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') + .then(client.funcs.checkFetchStatus) + .then(response => response.json()); + } catch (error) { + console.error(error); + } }, 3600000); if(!client.stations) { diff --git a/client/funcs/checkFetchStatus.js b/client/funcs/checkFetchStatus.js new file mode 100644 index 0000000..3b6c371 --- /dev/null +++ b/client/funcs/checkFetchStatus.js @@ -0,0 +1,7 @@ +module.exports = function (response) { + if (response.ok) { // res.status >= 200 && res.status < 300 + return response; + } else { + throw new Error(response.status + " " + response.statusText); + } +} \ No newline at end of file