From 18ac12953071a0db66d63a554a3b40f80fa3c681 Mon Sep 17 00:00:00 2001 From: Clarence Ho Date: Thu, 1 Feb 2018 15:14:58 -0800 Subject: [PATCH] Fix error handling for uploading attachment - For answerAttachService and attachService, fix the logic in uploader.onErrorItem --- .../modules/attachment/attachment-module.js | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/compair/static/modules/attachment/attachment-module.js b/compair/static/modules/attachment/attachment-module.js index 837d83f3f..60a070f03 100644 --- a/compair/static/modules/attachment/attachment-module.js +++ b/compair/static/modules/attachment/attachment-module.js @@ -214,14 +214,19 @@ module.service('attachService', } }; - uploader.onErrorItem = function(fileItem, response, status) { + uploader.onErrorItem = function(fileItem, response, status, headers) { fileItem.cancel(); fileItem.remove(); reset(); - if (response == '413') { + if (status == 413) { var upload_limit = UploadValidator.getAttachmentUploadLimit(); var limit_size = upload_limit / 1048576; // convert to MB Toaster.error("File Not Uploaded", "The file is larger than the "+limit_size.toFixed(0)+"MB maximum. Please upload a smaller file instead."); + } else if (response.title && response.message) { + Toast.error(response.title, response.message); + } else { + // e.g. network disconnected + Toaster.error("File Not Uploaded", "Please try again."); } }; @@ -304,18 +309,21 @@ module.service('answerAttachService', file = null; - uploader.onErrorItem = function() { - return function(fileItem, response, status) { - fileItem.cancel(); - fileItem.remove(); - reset(); - if (response == '413') { - var upload_limit = UploadValidator.getAttachmentUploadLimit(); - var limit_size = upload_limit / 1048576; // convert to MB - Toaster.error("File Not Uploaded", "The file is larger than the "+limit_size.toFixed(0)+"MB maximum. Please upload a smaller file instead."); - } - }; - } + uploader.onErrorItem = function(fileItem, response, status, headers) { + fileItem.cancel(); + fileItem.remove(); + reset(); + if (status == 413) { + var upload_limit = UploadValidator.getAttachmentUploadLimit(); + var limit_size = upload_limit / 1048576; // convert to MB + Toaster.error("File Not Uploaded", "The file is larger than the "+limit_size.toFixed(0)+"MB maximum. Please upload a smaller file instead."); + } else if (response.title && response.message) { + Toast.error(response.title, response.message); + } else { + // e.g. network disconnected + Toaster.error("File Not Uploaded", "Please try again."); + } + }; uploader.onSuccessItem = function(fileItem, response) { var extension = fileItem.file.name.slice(fileItem.file.name.lastIndexOf('.') + 1).toLowerCase();