MOBILE-3657 ws: Add throw on failed status helper function

main
Pau Ferrer Ocaña 2021-04-01 08:57:18 +02:00
parent 8addd2074d
commit 9cdee6f68d
1 changed files with 16 additions and 0 deletions

View File

@ -232,6 +232,22 @@ export class CoreWSProvider {
return new CoreError(message);
}
/**
* It will check if response has failed and throw the propper error.
*
* @param response WS response.
* @param defaultMessage Message to be used in case warnings is empty.
*/
throwOnFailedStatus(response: CoreStatusWithWarningsWSResponse, defaultMessage: string): void {
if (!response.status) {
if (response.warnings && response.warnings.length) {
throw new CoreWSError(response.warnings[0]);
}
throw new CoreError(defaultMessage);
}
}
/**
* Downloads a file from Moodle using Cordova File API.
*