mirror of
https://git.proxmox.com/git/proxmox-widget-toolkit
synced 2025-05-03 02:16:36 +00:00
utils: async api: reject with whole failure response and add helper to alert it
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
ea2b3fd736
commit
106fe29ef2
22
src/Utils.js
22
src/Utils.js
@ -443,6 +443,17 @@ utilities: {
|
|||||||
Ext.Ajax.request(newopts);
|
Ext.Ajax.request(newopts);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// can be useful for catching displaying errors from the API, e.g.:
|
||||||
|
// Proxmox.Async.api2({
|
||||||
|
// ...
|
||||||
|
// }).catch(Proxmox.Utils.alertResponseFailure);
|
||||||
|
alertResponseFailure: (response) => {
|
||||||
|
Ext.Msg.alert(
|
||||||
|
gettext('Error'),
|
||||||
|
response.htmlStatus || response.result.message,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
checked_command: function(orig_cmd) {
|
checked_command: function(orig_cmd) {
|
||||||
Proxmox.Utils.API2Request(
|
Proxmox.Utils.API2Request(
|
||||||
{
|
{
|
||||||
@ -1041,18 +1052,13 @@ utilities: {
|
|||||||
Ext.define('Proxmox.Async', {
|
Ext.define('Proxmox.Async', {
|
||||||
singleton: true,
|
singleton: true,
|
||||||
|
|
||||||
// Returns a Promise resolving to the result of an `API2Request`.
|
// Returns a Promise resolving to the result of an `API2Request` or rejecting to the error
|
||||||
|
// repsonse on failure
|
||||||
api2: function(reqOpts) {
|
api2: function(reqOpts) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
delete reqOpts.callback; // not allowed in this api
|
delete reqOpts.callback; // not allowed in this api
|
||||||
reqOpts.success = response => resolve(response);
|
reqOpts.success = response => resolve(response);
|
||||||
reqOpts.failure = response => {
|
reqOpts.failure = response => reject(response);
|
||||||
if (response.result && response.result.message) {
|
|
||||||
reject(response.result.message);
|
|
||||||
} else {
|
|
||||||
reject("api call failed");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Proxmox.Utils.API2Request(reqOpts);
|
Proxmox.Utils.API2Request(reqOpts);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user