2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

fix(http): parse json only if 200, else throw

- also trigger ajax events
This commit is contained in:
Faris Ansari 2018-09-30 18:44:40 +05:30
parent 96292f92c0
commit c23e8af2eb

View File

@ -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) {