mirror of
https://github.com/frappe/books.git
synced 2024-11-10 15:50:56 +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 frappe = require('frappejs');
|
||||||
const Observable = require('frappejs/utils/observable');
|
const Observable = require('frappejs/utils/observable');
|
||||||
|
const triggerEvent = name => frappe.events.trigger(`http:${name}`);
|
||||||
|
|
||||||
module.exports = class HTTPClient extends Observable {
|
module.exports = class HTTPClient extends Observable {
|
||||||
constructor({ server, protocol = 'http' }) {
|
constructor({ server, protocol = 'http' }) {
|
||||||
@ -105,19 +106,23 @@ module.exports = class HTTPClient extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetch(url, args) {
|
async fetch(url, args) {
|
||||||
|
triggerEvent('ajaxStart');
|
||||||
|
|
||||||
args.headers = this.getHeaders();
|
args.headers = this.getHeaders();
|
||||||
let response = await frappe.fetch(url, args);
|
let response = await frappe.fetch(url, args);
|
||||||
|
|
||||||
|
triggerEvent('ajaxStop');
|
||||||
|
|
||||||
|
if (response.status === 200) {
|
||||||
|
let data = await response.json();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
frappe.events.trigger('Unauthorized');
|
triggerEvent('unauthorized');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.status !== 200) {
|
throw Error(await response.text());
|
||||||
throw Error(data.error);
|
|
||||||
}
|
|
||||||
let data = await response.json();
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getFilesToUpload(doc) {
|
getFilesToUpload(doc) {
|
||||||
|
Loading…
Reference in New Issue
Block a user