🚸 Better error handling for uploading and removing files and folders (#4588)
* Notify user if file or folder removal fails * Improve error messages for failed file or folder removal * Notify user if file is too large to upload * Improve notification for failed file and folder uploads
This commit is contained in:
1 parent
1eaa88aa34
commit
45dab6b10c
5 files changed
+104
-20
No files matched your search
@@ -740,7 +740,7 @@ class LocalFileStorage(StorageInterface):
|
||||
):
|
||||
raise StorageError(
|
||||
f"{destination_name} does already exist in {destination_path}",
|
||||
code=StorageError.INVALID_DESTINATION,
|
||||
code=StorageError.ALREADY_EXISTS,
|
||||
)
|
||||
|
||||
source_meta = self._get_metadata_entry(source_path, source_name)
|
||||
|
||||
@@ -18,6 +18,7 @@ import octoprint.slicing
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.events import Events
|
||||
from octoprint.filemanager.destinations import FileDestinations
|
||||
from octoprint.filemanager.storage import StorageError
|
||||
from octoprint.server import (
|
||||
NO_CONTENT,
|
||||
current_user,
|
||||
@@ -660,11 +661,8 @@ def uploadGcodeFile(target):
|
||||
allow_overwrite=True,
|
||||
display=canonFilename,
|
||||
)
|
||||
except octoprint.filemanager.storage.StorageError as e:
|
||||
if e.code == octoprint.filemanager.storage.StorageError.INVALID_FILE:
|
||||
abort(415, description="Could not upload file, invalid type")
|
||||
else:
|
||||
abort(500, description="Could not upload file")
|
||||
except (OSError, StorageError) as e:
|
||||
_abortWithException(e)
|
||||
else:
|
||||
filename = fileProcessingFinished(
|
||||
added_file,
|
||||
@@ -776,11 +774,8 @@ def uploadGcodeFile(target):
|
||||
added_folder = fileManager.add_folder(
|
||||
target, futureFullPath, display=canonName
|
||||
)
|
||||
except octoprint.filemanager.storage.StorageError as e:
|
||||
if e.code == octoprint.filemanager.storage.StorageError.INVALID_DIRECTORY:
|
||||
abort(400, description="Could not create folder, invalid directory")
|
||||
else:
|
||||
abort(500, description="Could not create folder")
|
||||
except (OSError, StorageError) as e:
|
||||
_abortWithException(e)
|
||||
|
||||
location = url_for(
|
||||
".readGcodeFile",
|
||||
@@ -1235,7 +1230,10 @@ def deleteGcodeFile(filename, target):
|
||||
if target == FileDestinations.SDCARD:
|
||||
printer.delete_sd_file(filename, tags={"source:api", "api:files.sd"})
|
||||
else:
|
||||
fileManager.remove_file(target, filename)
|
||||
try:
|
||||
fileManager.remove_file(target, filename)
|
||||
except (OSError, StorageError) as e:
|
||||
_abortWithException(e)
|
||||
|
||||
elif _verifyFolderExists(target, filename):
|
||||
if _isBusy(target, filename):
|
||||
@@ -1254,11 +1252,44 @@ def deleteGcodeFile(filename, target):
|
||||
printer.unselect_file()
|
||||
|
||||
# delete it
|
||||
fileManager.remove_folder(target, filename, recursive=True)
|
||||
try:
|
||||
fileManager.remove_folder(target, filename, recursive=True)
|
||||
except (OSError, StorageError) as e:
|
||||
_abortWithException(e)
|
||||
|
||||
return NO_CONTENT
|
||||
|
||||
|
||||
def _abortWithException(error):
|
||||
if type(error) is StorageError:
|
||||
logging.getLogger(__name__).error(
|
||||
f"{error.message}: {error.code}", exc_info=error.cause
|
||||
)
|
||||
if error.code == StorageError.INVALID_DIRECTORY:
|
||||
abort(400, description="Could not create folder, invalid directory")
|
||||
elif error.code == StorageError.INVALID_FILE:
|
||||
abort(415, description="Could not upload file, invalid type")
|
||||
elif error.code == StorageError.INVALID_SOURCE:
|
||||
abort(404, description="Source path does not exist, invalid source")
|
||||
elif error.code == StorageError.INVALID_DESTINATION:
|
||||
abort(400, description="Destination is invalid")
|
||||
elif error.code == StorageError.DOES_NOT_EXIST:
|
||||
abort(404, description="Does not exit")
|
||||
elif error.code == StorageError.ALREADY_EXISTS:
|
||||
abort(409, description="File or folder already exists")
|
||||
elif error.code == StorageError.SOURCE_EQUALS_DESTINATION:
|
||||
abort(400, description="Source and destination are the same folder")
|
||||
elif error.code == StorageError.NOT_EMPTY:
|
||||
abort(409, description="Folder is not empty")
|
||||
elif error.code == StorageError.UNKNOWN:
|
||||
abort(500, description=str(error.cause).split(":")[0])
|
||||
else:
|
||||
abort(500, description=error.message)
|
||||
else:
|
||||
logging.getLogger(__name__).exception(error)
|
||||
abort(500, description=str(error).split(":")[0])
|
||||
|
||||
|
||||
def _getCurrentFile():
|
||||
currentJob = printer.get_current_job()
|
||||
if (
|
||||
|
||||
@@ -888,8 +888,27 @@ $(function () {
|
||||
deferred.reject();
|
||||
});
|
||||
})
|
||||
.fail(function () {
|
||||
.fail(function (data) {
|
||||
deferred.reject();
|
||||
|
||||
// Notify user
|
||||
var error =
|
||||
"<p>" +
|
||||
gettext(
|
||||
"Could not remove entry. Please check octoprint.log for possible reasons."
|
||||
) +
|
||||
"</p>";
|
||||
if (data.responseJSON && data.responseJSON.error) {
|
||||
error += pnotifyAdditionalInfo(
|
||||
"<pre>" + _.escape(data.responseJSON.error) + "</pre>"
|
||||
);
|
||||
}
|
||||
new PNotify({
|
||||
title: gettext("Failed to remove entry"),
|
||||
text: error,
|
||||
type: "error",
|
||||
hide: false
|
||||
});
|
||||
});
|
||||
|
||||
return deferred.promise().always(function () {
|
||||
@@ -1584,6 +1603,7 @@ $(function () {
|
||||
self._handleUploadAdd = function (e, data) {
|
||||
var file = data.files[0];
|
||||
var path = self.currentPath();
|
||||
var fileSizeTooBig = file.size > self.freeSpace();
|
||||
|
||||
var formData = {};
|
||||
if (path !== "") {
|
||||
@@ -1600,10 +1620,13 @@ $(function () {
|
||||
name: file.name
|
||||
})
|
||||
);
|
||||
$("p, form", self.uploadExistsDialog).toggle(!fileSizeTooBig);
|
||||
$("span", self.uploadExistsDialog).toggle(fileSizeTooBig);
|
||||
$("input", self.uploadExistsDialog)
|
||||
.val("")
|
||||
.prop("placeholder", response.suggestion);
|
||||
$("a.upload-rename", self.uploadExistsDialog)
|
||||
.toggle(!fileSizeTooBig)
|
||||
.prop("disabled", false)
|
||||
.off("click")
|
||||
.on("click", function () {
|
||||
@@ -1659,8 +1682,35 @@ $(function () {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
data.formData = formData;
|
||||
data.submit();
|
||||
if (fileSizeTooBig) {
|
||||
var error =
|
||||
"<p>" +
|
||||
gettext(
|
||||
"Could not upload the file. There is not enough disk space remaining."
|
||||
) +
|
||||
"</p>";
|
||||
|
||||
error +=
|
||||
"<pre>" +
|
||||
_.sprintf(gettext("Free Space: %(freespace)s"), {
|
||||
freespace: self.freeSpaceString()
|
||||
}) +
|
||||
"<br>" +
|
||||
_.sprintf(gettext("File Size: %(filesize)s"), {
|
||||
filesize: formatSize(file.size)
|
||||
}) +
|
||||
"</pre>";
|
||||
|
||||
new PNotify({
|
||||
title: "Upload failed",
|
||||
text: error,
|
||||
type: "error",
|
||||
hide: false
|
||||
});
|
||||
} else {
|
||||
data.formData = formData;
|
||||
data.submit();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1729,9 +1779,9 @@ $(function () {
|
||||
}
|
||||
error += "</p>";
|
||||
|
||||
if (data.jqXHR.responseText) {
|
||||
if (data.jqXHR.responseJSON && data.jqXHR.responseJSON.error) {
|
||||
error += pnotifyAdditionalInfo(
|
||||
"<pre>" + _.escape(data.jqXHR.responseText) + "</pre>"
|
||||
"<pre>" + _.escape(data.jqXHR.responseJSON.error) + "</pre>"
|
||||
);
|
||||
}
|
||||
new PNotify({
|
||||
|
||||
@@ -64,6 +64,9 @@
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<span hidden>
|
||||
{{ _('There is not enough free disk space to keep the existing file and upload this one.') }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="javascript:void(0)" class="btn" data-dismiss="modal" aria-hidden="true">{{ _('Cancel') }}</a>
|
||||
|
||||
@@ -292,7 +292,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
getattr(self.storage, operation)("bp_case.stl", "test/crazyradio.stl")
|
||||
self.fail("Expected an exception")
|
||||
except StorageError as e:
|
||||
self.assertEqual(e.code, StorageError.INVALID_DESTINATION)
|
||||
self.assertEqual(e.code, StorageError.ALREADY_EXISTS)
|
||||
|
||||
def test_add_folder(self):
|
||||
self._add_and_verify_folder("test", "test")
|
||||
@@ -481,7 +481,7 @@ class LocalStorageTest(unittest.TestCase):
|
||||
getattr(self.storage, operation)("source", "destination/copied")
|
||||
self.fail("Expected an exception")
|
||||
except StorageError as e:
|
||||
self.assertEqual(e.code, StorageError.INVALID_DESTINATION)
|
||||
self.assertEqual(e.code, StorageError.ALREADY_EXISTS)
|
||||
|
||||
def test_list(self):
|
||||
bp_case_stl = self._add_and_verify_file(
|
||||
|
||||
Reference in new issue
Block a user