diff --git a/src/core/services/ws.ts b/src/core/services/ws.ts index 13992fb2a..34543069e 100644 --- a/src/core/services/ws.ts +++ b/src/core/services/ws.ts @@ -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. *