@@ -1401,8 +1401,6 @@ function with_req(method, path, body, fun, on404fun) {
14011401 location . reload ( ) ;
14021402 return ;
14031403 }
1404-
1405- const full_page_404 = ! on404fun
14061404 var req = xmlHttpRequest ( ) ;
14071405 req . open ( method , 'api' + path , true ) ;
14081406 var header = authorization_header ( ) ;
@@ -1416,12 +1414,10 @@ function with_req(method, path, body, fun, on404fun) {
14161414 if ( ix != - 1 ) {
14171415 outstanding_reqs . splice ( ix , 1 ) ;
14181416 }
1419- if ( check_bad_response ( req , full_page_404 ) ) {
1417+ if ( check_bad_response ( req , ! on404fun , on404fun ) ) {
14201418 last_successful_connect = new Date ( ) ;
14211419 fun ( req ) ;
1422- } else if ( req . status == 404 ) {
1423- on404fun ( JSON . parse ( req . responseText ) )
1424- }
1420+ }
14251421 }
14261422 } ;
14271423 outstanding_reqs . push ( req ) ;
@@ -1512,7 +1508,14 @@ function initiate_logout(oauth, error = "") {
15121508 clear_cookie_value ( 'auth' ) ;
15131509 renderWarningMessageInLoginStatus ( oauth , error ) ;
15141510}
1515- function check_bad_response ( req , full_page_404 ) {
1511+ /**
1512+ * Handle bad http response
1513+ * @param {* } req
1514+ * @param {* } full_page_404 In case of 404, reload entire html page with the error message
1515+ * @param {* } on404fun In case of 404, call this function or else show a popup error message
1516+ * @returns true if there was no bad response
1517+ */
1518+ function check_bad_response ( req , full_page_404 , on404fun ) {
15161519 // 1223 == 204 - see https://www.enhanceie.com/ie/bugs.asp
15171520 // MSIE7 and 8 appear to do this in response to HTTP 204.
15181521 if ( ( req . status >= 200 && req . status < 300 ) || req . status == 1223 ) {
@@ -1523,17 +1526,26 @@ function check_bad_response(req, full_page_404) {
15231526 replace_content ( 'main' , html ) ;
15241527 }
15251528 else if ( req . status >= 400 && req . status <= 404 ) {
1526- var reason = JSON . parse ( req . responseText ) . reason ;
1527- if ( typeof ( reason ) != 'string' ) reason = JSON . stringify ( reason ) ;
1528-
1529- var error = JSON . parse ( req . responseText ) . error ;
1530- if ( typeof ( error ) != 'string' ) error = JSON . stringify ( error ) ;
1531-
1532- if ( error == 'bad_request' || error == 'not_found' || error == 'not_authorised' || error == 'not_authorized' ) {
1529+ let response = JSON . parse ( req . responseText ) ;
1530+ var error = response . error ;
1531+ if ( typeof ( error ) != 'string' ) {
1532+ error = JSON . stringify ( error ) ;
1533+ }
1534+ let reason = response . reason ;
1535+ if ( typeof ( reason ) != 'string' ) {
1536+ reason = JSON . stringify ( reason ) ;
1537+ }
1538+ if ( error == 'bad_request' ||
1539+ error == 'not_found' ||
1540+ reason == 'Not Found' ||
1541+ error == 'not_authorised' ||
1542+ error == 'not_authorized' ) {
15331543 if ( ( req . status == 401 || req . status == 403 ) && oauth . enabled ) {
1534- initiate_logout ( oauth , reason ) ;
1544+ initiate_logout ( oauth , reason ) ;
1545+ } else if ( on404fun && ( typeof on404fun === 'function' ) && req . status == 404 ) {
1546+ on404fun ( JSON . parse ( req . responseText ) ) ;
15351547 } else {
1536- show_popup ( 'warn' , fmt_escape_html ( reason ) ) ;
1548+ show_popup ( 'warn' , fmt_escape_html ( reason ) ) ;
15371549 }
15381550 } else if ( error == 'page_out_of_range' ) {
15391551 var seconds = 60 ;
0 commit comments