From 818360e2cf6b9cd8005af0df0b58eb279d23ccc3 Mon Sep 17 00:00:00 2001 From: Kyle Vonderwerth Date: Mon, 1 Oct 2018 01:59:02 -0700 Subject: [PATCH] Exception handling added for minion.py Exception handling for file io and os errors. --- pydfs/minion.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/pydfs/minion.py b/pydfs/minion.py index 8cf0513..d854579 100644 --- a/pydfs/minion.py +++ b/pydfs/minion.py @@ -11,18 +11,28 @@ class exposed_Minion(): blocks = {} def exposed_put(self,block_uuid,data,minions): - with open(DATA_DIR+str(block_uuid),'w') as f: - f.write(data) - if len(minions)>0: - self.forward(block_uuid,data,minions) + try: + with open(DATA_DIR+str(block_uuid),'w') as f: + f.write(data) + except IOError as e: + print "I/O error({0}): {1}".format(e.errno, e.strerror) + except: + print "Unexpected error:", sys.exc_info()[0] + if len(minions)>0: + self.forward(block_uuid,data,minions) def exposed_get(self,block_uuid): block_addr=DATA_DIR+str(block_uuid) if not os.path.isfile(block_addr): return None - with open(block_addr) as f: - return f.read() + try: + with open(block_addr) as f: + return f.read() + except IOError as e: + print "I/O error({0}): {1}".format(e.errno, e.strerror) + except: # handle other exceptions such as attribute errors + print "Unexpected error:", sys.exc_info()[0] def forward(self,block_uuid,data,minions): print "8888: forwaring to:" @@ -39,6 +49,10 @@ def delete_block(self,uuid): pass if __name__ == "__main__": - if not os.path.isdir(DATA_DIR): os.mkdir(DATA_DIR) + try: + if not os.path.isdir(DATA_DIR): os.mkdir(DATA_DIR) + except OSError as e: + print "OS error({0}): {1}".format(e.errno, e.strerror) + t = ThreadedServer(MinionService, port = 8888) - t.start() \ No newline at end of file + t.start()