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