From 55178101e85ec80b367cd88f84e251c4278c61ca Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Thu, 6 Jan 2022 12:48:06 +0530 Subject: [PATCH 1/3] fix: catch exceptions while parsing json on request failure --- frappe/public/js/frappe/request.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frappe/public/js/frappe/request.js b/frappe/public/js/frappe/request.js index eff0391338..87309f4226 100644 --- a/frappe/public/js/frappe/request.js +++ b/frappe/public/js/frappe/request.js @@ -292,8 +292,15 @@ frappe.request.call = function(opts) { .fail(function(xhr, textStatus) { try { if (xhr.responseText) { - var data = JSON.parse(xhr.responseText); - if (data.exception) { + var data; + try { + data = JSON.parse(xhr.responseText); + } catch (e) { + console.log("Unable to parse reponse text"); + console.log(xhr.responseText); + console.log(e); + } + if (data && data.exception) { // frappe.exceptions.CustomError -> CustomError var exception = data.exception.split('.').at(-1); var exception_handler = exception_handlers[exception]; From 307c7ced69572ec56051f3a88e1e91422d2e94fb Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Fri, 7 Jan 2022 14:55:54 +0530 Subject: [PATCH 2/3] chore: check custom exception handlers only for json response --- frappe/public/js/frappe/request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/request.js b/frappe/public/js/frappe/request.js index 87309f4226..16b113f079 100644 --- a/frappe/public/js/frappe/request.js +++ b/frappe/public/js/frappe/request.js @@ -291,7 +291,7 @@ frappe.request.call = function(opts) { }) .fail(function(xhr, textStatus) { try { - if (xhr.responseText) { + if (xhr.getResponseHeader('content-type') == 'application/json' && xhr.responseText) { var data; try { data = JSON.parse(xhr.responseText); From 1bde3b48bdd7a0d81f6d183b19f58ac8ecb8b72c Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Fri, 7 Jan 2022 14:56:25 +0530 Subject: [PATCH 3/3] fix: parsing exception from traceback --- frappe/public/js/frappe/request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frappe/public/js/frappe/request.js b/frappe/public/js/frappe/request.js index 16b113f079..35f946df65 100644 --- a/frappe/public/js/frappe/request.js +++ b/frappe/public/js/frappe/request.js @@ -301,8 +301,8 @@ frappe.request.call = function(opts) { console.log(e); } if (data && data.exception) { - // frappe.exceptions.CustomError -> CustomError - var exception = data.exception.split('.').at(-1); + // frappe.exceptions.CustomError: (1024, ...) -> CustomError + var exception = data.exception.split('.').at(-1).split(':').at(0); var exception_handler = exception_handlers[exception]; if (exception_handler) { exception_handler(data);