Skip to content

Commit 10b5386

Browse files
author
Erik Allik
committed
Merge pull request #22 from cizra/fileref-deletion-errors
Added error handling for fileref deletion
2 parents 1daa29b + 6802dcb commit 10b5386

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

spinoff/contrib/filetransfer/fileref.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ def upload(self, url, method='POST', expect_response=200):
115115
raise UploadFailed("Bad method: %s" % method)
116116

117117
def delete(self):
118-
if not self.server.ask(('delete', self.file_id)):
119-
raise FileNotFound
118+
ok, resp = self.server.ask(('delete', self.file_id))
119+
if not ok:
120+
reason, arg = resp
121+
if reason == 'file-not-found':
122+
raise FileNotFound
123+
elif reason == 'exception':
124+
raise DeletionFailed(arg)
120125

121126
def _transfer(self, fh, on_progress):
122127
request = get_context().spawn(Request.using(server=self.server, file_id=self.file_id, size=self.size, abstract_path=self.abstract_path))
@@ -156,6 +161,10 @@ class UploadFailed(Exception):
156161
pass
157162

158163

164+
class DeletionFailed(Exception):
165+
pass
166+
167+
159168
def move_or_copy(src, dst):
160169
try:
161170
os.rename(src, dst)

spinoff/contrib/filetransfer/server.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ def receive(self, msg):
7575
elif ('delete', ANY) == msg:
7676
_, file_id = msg
7777
if file_id not in self.published:
78-
self.reply(False)
78+
self.reply((False, ('file-not-found', None)))
7979
else:
8080
file_path, _ = self.published[file_id]
8181
del self.published[file_id]
82-
os.unlink(file_path)
83-
self.reply(True)
82+
try:
83+
os.unlink(file_path)
84+
except BaseException as e:
85+
self.reply((False, ('exception', (type(e).__name__, e.message, traceback.format_exc()))))
86+
else:
87+
self.reply((True, None))
8488

8589
def _touch_file(self, file_id):
8690
file_path, time_added = self.published[file_id]

0 commit comments

Comments
 (0)