[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [oonib/master] Add proper error handling in deck & input & nettest & policy
commit f2889515ec044dbaaa5b5795ffca00cd5020aff9
Author: Arturo Filastò <art@xxxxxxxxx>
Date: Mon Aug 19 14:20:57 2013 +0200
Add proper error handling in deck & input & nettest & policy
* Refactoring of handler related code
---
oonib/api.py | 2 --
oonib/bouncer/handlers.py | 4 ----
oonib/deck/handlers.py | 38 +++++++++++++++++++++++++-------------
oonib/input/handlers.py | 34 +++++++++++++++++++---------------
oonib/nettest/handlers.py | 20 +++++++++++---------
oonib/policy/handlers.py | 4 ++--
6 files changed, 57 insertions(+), 45 deletions(-)
diff --git a/oonib/api.py b/oonib/api.py
index 384b949..622df4d 100644
--- a/oonib/api.py
+++ b/oonib/api.py
@@ -23,6 +23,4 @@ if config.main.policy_file:
if config.main.bouncer_file:
ooniBackendAPI += bouncerAPI
-print ooniBackendAPI
-
ooniBackend = web.Application(ooniBackendAPI, debug=True)
diff --git a/oonib/bouncer/handlers.py b/oonib/bouncer/handlers.py
index bc81ddb..230f618 100644
--- a/oonib/bouncer/handlers.py
+++ b/oonib/bouncer/handlers.py
@@ -22,10 +22,6 @@ class BouncerQueryHandler(OONIBHandler):
'helper-address': helperAddress
})
- def get(self):
- #XXX unused
- pass
-
def post(self):
try:
query = json.loads(self.request.body)
diff --git a/oonib/deck/handlers.py b/oonib/deck/handlers.py
index 1b67a3f..d4b3bbe 100644
--- a/oonib/deck/handlers.py
+++ b/oonib/deck/handlers.py
@@ -9,30 +9,42 @@ from oonib import config, log
class DeckDescHandler(OONIBHandler):
def get(self, deckID):
+ # note:
+ # we don't have to sanitize deckID, because it's already checked
+ # against matching a certain pattern in the handler.
bn = os.path.basename(deckID)
try:
- f = open(os.path.join(config.main.deck_dir, bn))
- a = {}
- deckDesc = yaml.safe_load(f)
- for k in ['name', 'description', 'version', 'author', 'date']:
- a[k] = deckDesc[k]
- self.write(json.dumps(a))
+ with open(os.path.join(config.main.deck_dir, bn)) as f:
+ response = {}
+ deckDesc = yaml.safe_load(f)
+ for k in ['name', 'description', 'version', 'author', 'date']:
+ response[k] = deckDesc[k]
+ self.write(response)
except IOError:
log.err("Deck %s missing" % deckID)
+ self.set_status(404)
+ self.write({'error': 'missing-deck'})
except KeyError:
+ self.set_status(400)
log.err("Deck %s missing required keys!" % deckID)
+ self.write({'error': 'missing-deck-keys'})
class DeckListHandler(OONIBHandler):
def get(self):
- if not config.main.deck_dir: return
+ if not config.main.deck_dir:
+ self.set_status(501)
+ self.write({'error': 'no-decks-configured'})
+ return
path = os.path.abspath(config.main.deck_dir) + "/*"
decknames = map(os.path.basename, glob.iglob(path))
decknames = filter(lambda y: re.match("[a-z0-9]{40}", y), decknames)
deckList = []
for deckname in decknames:
- f = open(os.path.join(config.main.deck_dir, deckname))
- d = yaml.safe_load(f)
- deckList.append({'id': deckname,'name': d['name'],
- 'description': d['description']})
- f.close()
- self.write(json.dumps(deckList))
+ with open(os.path.join(config.main.deck_dir, deckname)) as f:
+ d = yaml.safe_load(f)
+ deckList.append({
+ 'id': deckname,
+ 'name': d['name'],
+ 'description': d['description']
+ })
+ self.write(deckList)
diff --git a/oonib/input/handlers.py b/oonib/input/handlers.py
index 8db1482..ed4c6b2 100644
--- a/oonib/input/handlers.py
+++ b/oonib/input/handlers.py
@@ -10,16 +10,21 @@ class InputDescHandler(OONIBHandler):
def get(self, inputID):
bn = os.path.basename(inputID) + ".desc"
try:
- f = open(os.path.join(config.main.input_dir, bn))
- a = {}
- inputDesc = yaml.safe_load(f)
- for k in ['name', 'description', 'version', 'author', 'date']:
- a[k] = inputDesc[k]
- self.write(json.dumps(a))
+ with open(os.path.join(config.main.input_dir, bn)) as f:
+ response = {}
+ inputDesc = yaml.safe_load(f)
+ for k in ['name', 'description', 'version', 'author', 'date']:
+ response[k] = inputDesc[k]
+ self.write(response)
except IOError:
log.err("No Input Descriptor found for id %s" % inputID)
+ self.set_status(404)
+ self.write({'error': 'missing-input'})
+
except Exception, e:
log.err("Invalid Input Descriptor found for id %s" % inputID)
+ self.set_status(500)
+ self.write({'error': 'invalid-input-descriptor'})
class InputListHandler(OONIBHandler):
def get(self):
@@ -27,12 +32,11 @@ class InputListHandler(OONIBHandler):
inputnames = map(os.path.basename, glob.iglob(path))
inputList = []
for inputname in inputnames:
- f = open(os.path.join(config.main.input_dir, inputname))
- d = yaml.safe_load(f)
- inputList.append({
- 'id': inputname,
- 'name': d['name'],
- 'description': d['description']
- })
- f.close()
- self.write(json.dumps(inputList))
+ with open(os.path.join(config.main.input_dir, inputname)) as f:
+ d = yaml.safe_load(f)
+ inputList.append({
+ 'id': inputname,
+ 'name': d['name'],
+ 'description': d['description']
+ })
+ self.write(inputList)
diff --git a/oonib/nettest/handlers.py b/oonib/nettest/handlers.py
index 17d5590..b3ceed4 100644
--- a/oonib/nettest/handlers.py
+++ b/oonib/nettest/handlers.py
@@ -10,16 +10,18 @@ class NetTestDescHandler(OONIBHandler):
def get(self, netTestID):
bn = os.path.basename(netTestID) + ".desc"
try:
- f = open(os.path.join(config.main.nettest_dir, bn))
- a = {}
- netTestDesc = yaml.safe_load(f)
- for k in ['name', 'description', 'version', 'author', 'date']:
- a[k] = netTestDesc[k]
- self.write(json.dumps(a))
+ with open(os.path.join(config.main.nettest_dir, bn)) as f:
+ response = {}
+ netTestDesc = yaml.safe_load(f)
+ for k in ['name', 'description', 'version', 'author', 'date']:
+ response[k] = netTestDesc[k]
+ self.write(response)
except IOError:
log.err("No NetTest Descriptor found for id %s" % netTestID)
+ self.set_status(404)
+ self.write({'error': 'missing-nettest'})
+
except Exception, e:
log.err("Invalid NetTest Descriptor found for id %s" % netTestID)
-
-
-
+ self.set_status(500)
+ self.write({'error': 'invalid-nettest-descriptor'})
diff --git a/oonib/policy/handlers.py b/oonib/policy/handlers.py
index f72e746..788e578 100644
--- a/oonib/policy/handlers.py
+++ b/oonib/policy/handlers.py
@@ -12,7 +12,7 @@ class NetTestPolicyHandler(OONIBHandler):
"""
with open(config.main.policy_file) as f:
p = yaml.safe_load(f)
- self.write(json.dumps(p['nettest']))
+ self.write(p['nettest'])
class InputPolicyHandler(OONIBHandler):
def get(self):
@@ -21,4 +21,4 @@ class InputPolicyHandler(OONIBHandler):
"""
with open(config.main.policy_file) as f:
p = yaml.safe_load(f)
- self.write(json.dumps(p['input']))
+ self.write(p['input'])
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits