From 9cdee6f68de394e5b524f5bb5467ada8031cdf86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 1 Apr 2021 08:57:18 +0200 Subject: [PATCH] MOBILE-3657 ws: Add throw on failed status helper function --- src/core/services/ws.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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. *