mirror of
				https://github.com/SuperBFG7/ympd
				synced 2025-10-31 13:53:00 +00:00 
			
		
		
		
	feat: first error handling code for ajax requests
This commit is contained in:
		| @@ -1128,11 +1128,7 @@ function appendAfterQueue(type, uri, to, name) { | |||||||
|     switch(type) { |     switch(type) { | ||||||
|         case 'song': |         case 'song': | ||||||
|             sendAPI({"cmd": "MPD_API_ADD_TRACK_AFTER", "data": {"uri": uri, "to": to}}); |             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; |  | ||||||
|         case 'dir': |  | ||||||
|             sendAPI({"cmd": "MPD_API_ADD_TRACK_AFTER", "data": {"uri": uri, "to": to}}); |  | ||||||
|             showNotification('"' + name + '" added to pos ' + to, '', '', 'success'); |  | ||||||
|             break; |             break; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -1256,10 +1252,22 @@ function sendAPI(request, callback) { | |||||||
|     ajaxRequest.open('POST', '/api', true); |     ajaxRequest.open('POST', '/api', true); | ||||||
|     ajaxRequest.setRequestHeader('Content-type', 'application/json'); |     ajaxRequest.setRequestHeader('Content-type', 'application/json'); | ||||||
|     ajaxRequest.onreadystatechange = function() { |     ajaxRequest.onreadystatechange = function() { | ||||||
|         if (ajaxRequest.readyState == 4) |         if (ajaxRequest.readyState == 4) { | ||||||
|             if (callback != undefined && typeof(callback) == 'function') |             if (ajaxRequest.responseText != '') { | ||||||
|                 if (ajaxRequest.responseText != '') |                 var obj = JSON.parse(ajaxRequest.responseText); | ||||||
|                     callback(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)); |     ajaxRequest.send(JSON.stringify(request)); | ||||||
| } | } | ||||||
| @@ -1270,7 +1278,7 @@ function openLocalPlayer() { | |||||||
|  |  | ||||||
| function updateDB() { | function updateDB() { | ||||||
|     sendAPI({"cmd": "MPD_API_UPDATE_DB"}); |     sendAPI({"cmd": "MPD_API_UPDATE_DB"}); | ||||||
|     showNotification('Updating MPD Database...', '', '', 'success'); | //    showNotification('Updating MPD Database...', '', '', 'success'); | ||||||
| } | } | ||||||
|  |  | ||||||
| function clickPlay() { | function clickPlay() { | ||||||
| @@ -1426,7 +1434,7 @@ function showNotification(notificationTitle,notificationText,notificationHtml,no | |||||||
|             alertBox = document.getElementById('alertBox'); |             alertBox = document.getElementById('alertBox'); | ||||||
|         } |         } | ||||||
|         alertBox.classList.add('alert','alert-' + notificationType); |         alertBox.classList.add('alert','alert-' + notificationType); | ||||||
|         alertBox.innerHTML = '<div><strong>' + notificationTitle + '</strong>' + notificationHtml + '</div>'; |         alertBox.innerHTML = '<div><strong>' + notificationTitle + '</strong><br/>' + notificationHtml + '</div>'; | ||||||
|         document.getElementsByTagName('main')[0].append(alertBox); |         document.getElementsByTagName('main')[0].append(alertBox); | ||||||
|         document.getElementById('alertBox').classList.add('alertBoxActive'); |         document.getElementById('alertBox').classList.add('alertBoxActive'); | ||||||
|         if (alertTimeout) |         if (alertTimeout) | ||||||
|   | |||||||
| @@ -57,7 +57,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) | |||||||
|     size_t n = 0; |     size_t n = 0; | ||||||
|     char *cmd; |     char *cmd; | ||||||
|     unsigned int uint_buf1, uint_buf2; |     unsigned int uint_buf1, uint_buf2; | ||||||
|     int je, int_buf;  |     int je, int_buf, int_rc;  | ||||||
|     float float_buf; |     float float_buf; | ||||||
|     char *p_charbuf1, *p_charbuf2; |     char *p_charbuf1, *p_charbuf2; | ||||||
|     struct mympd_state { int a; int b; } state = { .a = 0, .b = 0 }; |     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; |             break; | ||||||
|         case MPD_API_ADD_TRACK_AFTER: |         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) { |             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); |                 free(p_charbuf1); | ||||||
|                 n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}"); |  | ||||||
|             } |             } | ||||||
|             break; |             break; | ||||||
|         case MPD_API_REPLACE_TRACK: |         case MPD_API_REPLACE_TRACK: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 jcorporation
					jcorporation