Skip to content

Commit

Permalink
Added error handling for fileref deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Elmo Todurov committed Oct 4, 2013
1 parent ab3a820 commit 6802dcb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 11 additions & 2 deletions spinoff/contrib/filetransfer/fileref.py
Expand Up @@ -115,8 +115,13 @@ def upload(self, url, method='POST', expect_response=200):
raise UploadFailed("Bad method: %s" % method)

def delete(self):
if not self.server.ask(('delete', self.file_id)):
raise FileNotFound
ok, resp = self.server.ask(('delete', self.file_id))
if not ok:
reason, arg = resp
if reason == 'file-not-found':
raise FileNotFound
elif reason == 'exception':
raise DeletionFailed(arg)

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


class DeletionFailed(Exception):
pass


def move_or_copy(src, dst):
try:
os.rename(src, dst)
Expand Down
10 changes: 7 additions & 3 deletions spinoff/contrib/filetransfer/server.py
Expand Up @@ -75,12 +75,16 @@ def receive(self, msg):
elif ('delete', ANY) == msg:
_, file_id = msg
if file_id not in self.published:
self.reply(False)
self.reply((False, ('file-not-found', None)))
else:
file_path, _ = self.published[file_id]
del self.published[file_id]
os.unlink(file_path)
self.reply(True)
try:
os.unlink(file_path)
except BaseException as e:
self.reply((False, ('exception', (type(e).__name__, e.message, traceback.format_exc()))))
else:
self.reply((True, None))

def _touch_file(self, file_id):
file_path, time_added = self.published[file_id]
Expand Down

0 comments on commit 6802dcb

Please sign in to comment.