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:
parent
96292f92c0
commit
c23e8af2eb
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user