From 39d63bde64d4c85e31faed9ba16c59d15f67ca4b Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Tue, 11 Jun 2024 12:53:39 +0530 Subject: [PATCH] #1420 - error handling for media upload request. --- packages/openchs-android/package.json | 2 +- .../src/framework/errorHandling/ErrorUtil.js | 4 ++++ .../src/framework/http/requests.js | 6 ++++- .../src/service/MediaQueueService.js | 23 +++++++++++-------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/openchs-android/package.json b/packages/openchs-android/package.json index 12416931f..92741d135 100644 --- a/packages/openchs-android/package.json +++ b/packages/openchs-android/package.json @@ -58,7 +58,7 @@ "lodash": "4.17.21", "moment": "2.29.4", "native-base": "3.4.9", - "openchs-models": "1.31.76", + "openchs-models": "1.31.80", "prop-types": "15.8.1", "react": "18.2.0", "react-native": "0.72.3", diff --git a/packages/openchs-android/src/framework/errorHandling/ErrorUtil.js b/packages/openchs-android/src/framework/errorHandling/ErrorUtil.js index 172117adb..4f2d6d604 100644 --- a/packages/openchs-android/src/framework/errorHandling/ErrorUtil.js +++ b/packages/openchs-android/src/framework/errorHandling/ErrorUtil.js @@ -49,6 +49,10 @@ class ErrorUtil { return error; }); } + + static newError(newMessage, error) { + return new Error(newMessage + ". " + error.message); + } } export default ErrorUtil; diff --git a/packages/openchs-android/src/framework/http/requests.js b/packages/openchs-android/src/framework/http/requests.js index 3f5da330b..a80406e39 100644 --- a/packages/openchs-android/src/framework/http/requests.js +++ b/packages/openchs-android/src/framework/http/requests.js @@ -18,10 +18,14 @@ const getIdpType = async () => { return await settingsService.getSettings().idpType; } +export function isHttpRequestSuccessful(responseCode) { + return ACCEPTABLE_RESPONSE_STATUSES.indexOf(responseCode) > -1; +} + const fetchFactory = (endpoint, method = "GET", params, fetchWithoutTimeout) => { const processResponse = (response) => { let responseCode = parseInt(response.status); - if (ACCEPTABLE_RESPONSE_STATUSES.indexOf(responseCode) > -1) { + if (isHttpRequestSuccessful(responseCode)) { return Promise.resolve(response); } if (responseCode === 403 || responseCode === 401) { diff --git a/packages/openchs-android/src/service/MediaQueueService.js b/packages/openchs-android/src/service/MediaQueueService.js index 511bfb233..0327f1801 100644 --- a/packages/openchs-android/src/service/MediaQueueService.js +++ b/packages/openchs-android/src/service/MediaQueueService.js @@ -1,9 +1,9 @@ import BaseService from "./BaseService"; import Service from "../framework/bean/Service"; -import {Encounter, Individual, MediaQueue, ProgramEncounter, ProgramEnrolment} from 'avni-models'; +import {Encounter, Individual, MediaQueue, ProgramEncounter, ProgramEnrolment} from 'openchs-models'; import General from "../utility/General"; import _ from 'lodash'; -import {get} from '../framework/http/requests'; +import {get, isHttpRequestSuccessful} from '../framework/http/requests'; import RNFetchBlob from 'rn-fetch-blob'; import FileSystem from "../model/FileSystem"; import fs from 'react-native-fs'; @@ -17,6 +17,14 @@ import I18n from 'i18n-js'; import ErrorUtil from "../framework/errorHandling/ErrorUtil"; const PARALLEL_UPLOAD_COUNT = 1; +function checkUploadStatus(response, mediaDisplayText) { + const statusCode = response.info().status; + if (!isHttpRequestSuccessful(statusCode)) { + throw new Error("Media upload failed. HTTP Status:" + statusCode + ". " + mediaDisplayText); + } + General.logDebug('MediaQueueService', `Upload of ${mediaDisplayText} done`); +} + @Service("mediaQueueService") class MediaQueueService extends BaseService { static DumpType = { @@ -118,10 +126,8 @@ class MediaQueueService extends BaseService { }, RNFetchBlob.wrap(this.getAbsoluteFileName(mediaQueueItem))); let jobTimeoutHandler = this.cancelUploadIfNoProgress(new Date(), uploadTask, mediaQueueItem.fileName) - uploadTask - .then(() => { - General.logDebug('MediaQueueService', `Upload of ${mediaQueueItem.uuid} - ${mediaQueueItem.fileName} done`); - }) + const returnPromise = uploadTask + .then((x) => checkUploadStatus(x, mediaQueueItem.getDisplayText())) .finally(() => clearTimeout(jobTimeoutHandler)); uploadTask.uploadProgress({interval: 1000},(sent, total) => { @@ -129,7 +135,7 @@ class MediaQueueService extends BaseService { clearTimeout(jobTimeoutHandler); jobTimeoutHandler = this.cancelUploadIfNoProgress(new Date(), uploadTask, mediaQueueItem.fileName); }); - return uploadTask; + return returnPromise; } cancelUploadIfNoProgress(lastProgressTime, uploadTask, fileName) { @@ -148,7 +154,7 @@ class MediaQueueService extends BaseService { .uploadProgress((written, total) => { General.logDebug("MediaQueueService", 'uploaded', written / total); cb(written, total); - }); + }).then((x) => checkUploadStatus(x, `${url}. ${fullFilePath}`)); } deleteFile(mediaQueueItem) { @@ -205,7 +211,6 @@ class MediaQueueService extends BaseService { .then(() => this.replaceObservation(mediaQueueItem, uploadUrl)) .then(() => this.popItem(mediaQueueItem)) .catch((error) => { - General.logError("MediaQueueService", `Error while uploading ${mediaQueueItem.uuid} - ${mediaQueueItem.fileName}`); General.logError("MediaQueueService", error); return Promise.reject(error); });