Pārlūkot izejas kodu

slightly less noisy error handling

raylu 11 gadi atpakaļ
vecāks
revīzija
80f9e5269a
1 mainītis faili ar 16 papildinājumiem un 9 dzēšanām
  1. 16 9
      api/server.py

+ 16 - 9
api/server.py

@@ -4,6 +4,7 @@ import errno
 import json
 import os
 from os import path
+import traceback
 import warnings
 
 warnings.filterwarnings('ignore', 'libevent')
@@ -26,16 +27,22 @@ def main():
 	server.serve_forever()
 
 def application(environ, start_response):
-	split = environ['PATH_INFO'][1:].split('/')
-	if split[0] == 'v1':
-		handler = handlers.get(split[2])
-		if handler:
-			start_response('200 OK', [('Content-type', 'text/plain')])
-			return [handler(split, environ)]
+	try:
+		split = environ['PATH_INFO'][1:].split('/')
+		if split[0] == 'v1':
+			handler = handlers.get(split[2])
+			if handler:
+				body = handler(split, environ)
+				start_response('200 OK', [('Content-type', 'text/plain')])
+				return [body]
+			else:
+				print 'no handler for', split
 		else:
-			print 'no handler for', split
-	else:
-		print 'split was', split
+			print 'split was', split
+	except:
+		traceback.print_exc()
+		start_response('500 Internal Server Error', [('Content-type', 'text/plain')])
+		return ['ruh roh']
 
 def get_data(split, environ):
 	group = int(split[1])