Skip to content

Commit

Permalink
Returning some exceptions in JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
ftsalamp authored and danielballan committed May 11, 2018
1 parent ee42eef commit 6de1d38
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion web_monitoring/diffing_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tornado.httpclient
import tornado.ioloop
import tornado.web
from tornado.stack_context import ExceptionStackContext
import traceback
import web_monitoring
import web_monitoring.differs
Expand All @@ -15,6 +16,9 @@
)
import web_monitoring.html_diff_render
import web_monitoring.links_diff
import json

import pdb

# Map tokens in the REST API to functions in modules.
# The modules do not have to be part of the web_monitoring package.
Expand Down Expand Up @@ -57,6 +61,15 @@

client = tornado.httpclient.AsyncHTTPClient()

def handle_request(response):
if response.error is not None:
try:
response.rethrow()
except ValueError:
print("This is a value error")

else:
print('Handle request')

class DiffHandler(tornado.web.RequestHandler):
# subclass must define `differs` attribute
Expand All @@ -77,7 +90,35 @@ def get(self, differ):
b = query_params.pop('b')

# Fetch server response for URLs a and b.
res_a, res_b = yield [client.fetch(a), client.fetch(b)]
res_a, res_b = yield [client.fetch(a, raise_error=False), client.fetch(b, raise_error=False)]

#Check if the HTTP requests were successfull and handle exeptions
if res_a.error is not None:
try:
res_a.rethrow()
except (ValueError, IOError):
pdb.set_trace()
data = {}
data['code'] = res_a.code
data['error'] = str(res_a.error)
data['parameter'] = 'a'
json_data = json.dumps(data)
self.write(json_data)
return

if res_b.error is not None:
try:
res_b.rethrow()
except (ValueError, IOError):
pdb.set_trace()
data = {}
data['code'] = res_b.code
data['error'] = str(res_b.error)
data['parameter'] = 'b'
json_data = json.dumps(data)
self.write(json_data)
return


# Validate response bytes against hash, if provided.
for query_param, res in zip(('a_hash', 'b_hash'), (res_a, res_b)):
Expand Down

0 comments on commit 6de1d38

Please sign in to comment.