diff myrss/myrss_app.py @ 70:3456dd3e8660

myrss: add exception handling around main()
author paulo
date Wed, 29 Jul 2015 00:20:39 -0600
parents ae0f2f438a95
children c7bbd3805509
line diff
     1.1 --- a/myrss/myrss_app.py	Thu Jun 11 22:03:34 2015 -0700
     1.2 +++ b/myrss/myrss_app.py	Wed Jul 29 00:20:39 2015 -0600
     1.3 @@ -6,10 +6,11 @@
     1.4  import Queue
     1.5  import datetime
     1.6  import time
     1.7 +import traceback
     1.8  
     1.9  import logging
    1.10  logging.basicConfig(
    1.11 -	level=logging.INFO,
    1.12 +	#level=logging.DEBUG,
    1.13  	#filename="_LOG",
    1.14  	#format="%(asctime)s %(levelname)-8s %(message)s",
    1.15  )
    1.16 @@ -249,11 +250,20 @@
    1.17  			WorkerThread(input_queue=self._iq, output_queue=self._oq).start()
    1.18  
    1.19  	def __call__(self, environ, start_response):
    1.20 -		response_body = main(self._iq, self._oq, self._main_lock)
    1.21 +		response_code = "500 Internal Server Error"
    1.22 +		response_type = "text/plain; charset=UTF-8"
    1.23 +
    1.24 +		try:
    1.25 +			response_body = main(self._iq, self._oq, self._main_lock)
    1.26 +			response_code = "200 OK"
    1.27 +			response_type = "text/html; charset=UTF-8"
    1.28 +		except:
    1.29 +			response_body = traceback.format_exc()
    1.30 +
    1.31  		response_headers = [
    1.32 -			("Content-Type", "text/html; charset=UTF-8"),
    1.33 +			("Content-Type", response_type),
    1.34  			("Content-Length", str(len(response_body))),
    1.35  		]
    1.36 -		start_response("200 OK", response_headers)
    1.37 +		start_response(response_code, response_headers)
    1.38  
    1.39  		return [response_body]