From 5d685904c4a705fa2ac2d9365074cba9ec918c4c Mon Sep 17 00:00:00 2001 From: jcorporation Date: Wed, 18 Jul 2018 23:29:19 +0100 Subject: [PATCH] feat: first error handling code for ajax requests --- htdocs/js/mpd.js | 30 +++++++++++++++++++----------- src/mpd_client.c | 13 +++++++++---- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index 2afed05..5450d49 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -1128,11 +1128,7 @@ function appendAfterQueue(type, uri, to, name) { switch(type) { case 'song': sendAPI({"cmd": "MPD_API_ADD_TRACK_AFTER", "data": {"uri": uri, "to": to}}); - showNotification('"' + name + '" added to pos ' + to, '', '', 'success'); - break; - case 'dir': - sendAPI({"cmd": "MPD_API_ADD_TRACK_AFTER", "data": {"uri": uri, "to": to}}); - showNotification('"' + name + '" added to pos ' + to, '', '', 'success'); +// showNotification('"' + name + '" added to pos ' + to, '', '', 'success'); break; } } @@ -1256,10 +1252,22 @@ function sendAPI(request, callback) { ajaxRequest.open('POST', '/api', true); ajaxRequest.setRequestHeader('Content-type', 'application/json'); ajaxRequest.onreadystatechange = function() { - if (ajaxRequest.readyState == 4) - if (callback != undefined && typeof(callback) == 'function') - if (ajaxRequest.responseText != '') - callback(JSON.parse(ajaxRequest.responseText)); + if (ajaxRequest.readyState == 4) { + if (ajaxRequest.responseText != '') { + var obj = JSON.parse(ajaxRequest.responseText); + if (obj.type == 'error') { + showNotification('Error', obj.data, obj.data, 'error'); + console.log('Error: ' + obj.data); + } + else if (obj.type == 'result' && obj.data != 'ok') + showNotification(obj.data, '', '', 'success'); + else if (callback != undefined && typeof(callback) == 'function') + callback(obj); + } + else { + console.log('Empty response for request: ' + JSON.stringify(request)); + } + } }; ajaxRequest.send(JSON.stringify(request)); } @@ -1270,7 +1278,7 @@ function openLocalPlayer() { function updateDB() { sendAPI({"cmd": "MPD_API_UPDATE_DB"}); - showNotification('Updating MPD Database...', '', '', 'success'); +// showNotification('Updating MPD Database...', '', '', 'success'); } function clickPlay() { @@ -1426,7 +1434,7 @@ function showNotification(notificationTitle,notificationText,notificationHtml,no alertBox = document.getElementById('alertBox'); } alertBox.classList.add('alert','alert-' + notificationType); - alertBox.innerHTML = '
' + notificationTitle + '' + notificationHtml + '
'; + alertBox.innerHTML = '
' + notificationTitle + '
' + notificationHtml + '
'; document.getElementsByTagName('main')[0].append(alertBox); document.getElementById('alertBox').classList.add('alertBoxActive'); if (alertTimeout) diff --git a/src/mpd_client.c b/src/mpd_client.c index 0eb7d35..31fb9b1 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -57,7 +57,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) size_t n = 0; char *cmd; unsigned int uint_buf1, uint_buf2; - int je, int_buf; + int je, int_buf, int_rc; float float_buf; char *p_charbuf1, *p_charbuf2; struct mympd_state { int a; int b; } state = { .a = 0, .b = 0 }; @@ -266,11 +266,16 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) } break; case MPD_API_ADD_TRACK_AFTER: - je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q, to:%u } }", &p_charbuf1, &uint_buf1); + je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q, to:%d } }", &p_charbuf1, &int_buf); +// if (int_buf == -1) +// int_buf = 1; if (je == 2) { - mpd_run_add_id_to(mpd.conn, p_charbuf1, uint_buf1); + int_rc = mpd_run_add_id_to(mpd.conn, p_charbuf1, int_buf); + if (int_rc == -1 ) + n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Cant add %s after track %d\"}", p_charbuf1, int_buf); + else + n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Added track %s after track %d\"}", p_charbuf1, int_buf); free(p_charbuf1); - n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}"); } break; case MPD_API_REPLACE_TRACK: