diff --git a/backends/http.js b/backends/http.js index 1a2816a5..48c7cfba 100644 --- a/backends/http.js +++ b/backends/http.js @@ -1,5 +1,6 @@ const frappe = require('frappejs'); const Observable = require('frappejs/utils/observable'); +const triggerEvent = name => frappe.events.trigger(`http:${name}`); module.exports = class HTTPClient extends Observable { constructor({ server, protocol = 'http' }) { @@ -105,19 +106,23 @@ module.exports = class HTTPClient extends Observable { } async fetch(url, args) { + triggerEvent('ajaxStart'); + args.headers = this.getHeaders(); let response = await frappe.fetch(url, args); + triggerEvent('ajaxStop'); + + if (response.status === 200) { + let data = await response.json(); + return data; + } + if (response.status === 401) { - frappe.events.trigger('Unauthorized'); + triggerEvent('unauthorized'); } - if (response.status !== 200) { - throw Error(data.error); - } - let data = await response.json(); - - return data; + throw Error(await response.text()); } getFilesToUpload(doc) {