Merge pull request #7717 from deepeshgarg007/error_handlers_hotfix

feat: Error handlers
This commit is contained in:
Rushabh Mehta 2019-06-18 15:49:47 +05:30 committed by GitHub
commit 54450ef6ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View file

@ -339,6 +339,10 @@ def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None,
out.alert = 1
message_log.append(json.dumps(out))
if raise_exception:
local.response['exc_type'] = raise_exception.__name__
_raise_exception()
def clear_messages():

View file

@ -4,6 +4,7 @@
// My HTTP Request
frappe.provide('frappe.request');
frappe.provide('frappe.request.error_handlers');
frappe.request.url = '/';
frappe.request.ajax_count = 0;
frappe.request.waiting_for_ajax = [];
@ -306,11 +307,23 @@ frappe.request.cleanup = function(opts, r) {
return;
}
// global error handlers
if (r.exc_type) {
let handlers = frappe.request.error_handlers[r.exc_type] || [];
handlers.forEach(handler => {
handler(r);
});
}
// show messages
if(r._server_messages && !opts.silent) {
r._server_messages = JSON.parse(r._server_messages);
frappe.hide_msgprint();
frappe.msgprint(r._server_messages);
let handlers = frappe.request.error_handlers[r.exc_type] || [];
// dont show server messages if their handlers exist
if (!handlers.length) {
r._server_messages = JSON.parse(r._server_messages);
frappe.hide_msgprint();
frappe.msgprint(r._server_messages);
}
}
// show errors
@ -450,6 +463,11 @@ frappe.request.cleanup_request_opts = function(request_opts) {
return request_opts;
};
frappe.request.on_error = function(error_type, handler) {
frappe.request.error_handlers[error_type] = frappe.request.error_handlers[error_type] || [];
frappe.request.error_handlers[error_type].push(handler);
}
$(document).ajaxSend(function() {
frappe.request.ajax_count++;
});