Skip to content

Commit

Permalink
#1420 - error handling for media upload request.
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Jun 11, 2024
1 parent 2eee568 commit 39d63bd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/openchs-android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ErrorUtil {
return error;
});
}

static newError(newMessage, error) {
return new Error(newMessage + ". " + error.message);
}
}

export default ErrorUtil;
6 changes: 5 additions & 1 deletion packages/openchs-android/src/framework/http/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 14 additions & 9 deletions packages/openchs-android/src/service/MediaQueueService.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = {
Expand Down Expand Up @@ -118,18 +126,16 @@ 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) => {
General.logDebug('MediaQueueService', `${mediaQueueItem.fileName} uploadProgress ${sent}/${total}`);
clearTimeout(jobTimeoutHandler);
jobTimeoutHandler = this.cancelUploadIfNoProgress(new Date(), uploadTask, mediaQueueItem.fileName);
});
return uploadTask;
return returnPromise;
}

cancelUploadIfNoProgress(lastProgressTime, uploadTask, fileName) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 39d63bd

Please sign in to comment.