From b3bfe116c2a8642e275f901c0b995d8469b8e709 Mon Sep 17 00:00:00 2001 From: MeowcaTheoRange Date: Wed, 21 Feb 2024 16:19:09 -0600 Subject: [PATCH] Error handling via notification --- index.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index c7b2c14..a3cdd42 100644 --- a/index.js +++ b/index.js @@ -186,15 +186,10 @@ async function uploadToNextcloud({ fileStream, url, name, channelName, tags, alb } async function main() { - const socket = io.connect(`http://localhost:${process.env.NOTIFICATION_SERVER_PORT}`, {reconnect: true}); - - socket.on('connect', function (s) { - console.log('Successfully connected to notification server'); - }); const video = await getVideo(); - if (video == null) return dismantle(socket); + if (video == null) return dismantle(); const nextcloudClient = createClient( `https://${process.env.NEXTCLOUD_INSTANCE}/remote.php/dav/files/${process.env.NEXTCLOUD_USERNAME}/`, @@ -204,10 +199,10 @@ async function main() { } ); - if (await checkNextcloud(video, nextcloudClient)) return dismantle(socket); + if (await checkNextcloud(video, nextcloudClient)) return dismantle(); socket.emit('nodeMessage', { - message: `New song found - ${video.name} from ${video.channelName}`, + message: `New song found - ${video.tags.title} by ${video.tags.artist}`, password: process.env.NOTIFICATION_SERVER_PASSWORD }) @@ -217,14 +212,14 @@ async function main() { } socket.emit('nodeMessage', { - message: `Cobalt download starting - ${video.name} from ${video.channelName}`, + message: `Cobalt download starting - ${video.tags.title} by ${video.tags.artist}`, password: process.env.NOTIFICATION_SERVER_PASSWORD }) const cobalt = await downloadFromCobalt(video); socket.emit('nodeMessage', { - message: `Cobalt download finished - ${video.name} from ${video.channelName}`, + message: `Cobalt download finished - ${video.tags.title} by ${video.tags.artist}`, password: process.env.NOTIFICATION_SERVER_PASSWORD }); @@ -234,7 +229,7 @@ async function main() { }); socket.emit('nodeMessage', { - message: `Nextcloud upload running - ${video.name} from ${video.channelName}`, + message: `Nextcloud upload running - ${video.tags.title} by ${video.tags.artist}`, password: process.env.NOTIFICATION_SERVER_PASSWORD }); @@ -248,16 +243,29 @@ async function main() { ); socket.emit('nodeMessage', { - message: `Nextcloud upload finished - ${video.name} from ${video.channelName}`, + message: `Nextcloud upload finished - ${video.tags.title} by ${video.tags.artist}`, password: process.env.NOTIFICATION_SERVER_PASSWORD }) - return dismantle(socket); + return dismantle(); } -main(); +const socket = io.connect(`http://localhost:${process.env.NOTIFICATION_SERVER_PORT}`, {reconnect: true}); -function dismantle(socket) { +socket.on('connect', function (s) { + console.log('Successfully connected to notification server'); + try { + main(); + } catch (err) { + socket.emit('nodeMessage', { + message: `LastFMDownloader error - ${err}`, + password: process.env.NOTIFICATION_SERVER_PASSWORD + }) + throw err; + } +}); + +function dismantle() { socket.disconnect(); return null; } \ No newline at end of file