Skip to content

Commit 207d939

Browse files
committed
invalid json post data now more gracefully dealt with
1 parent 7301d6b commit 207d939

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

gurgleapps_webserver.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def add_function_route(self, route, function):
9090
async def serve_request(self, reader, writer):
9191
gc.collect()
9292
try:
93+
response = Response(writer)
9394
url = ""
9495
method = ""
9596
content_length = 0
@@ -130,9 +131,14 @@ async def serve_request(self, reader, writer):
130131
if content_length > 0:
131132
post_data_raw = await reader.readexactly(content_length)
132133
print("POST data:", post_data_raw)
133-
post_data = json.loads(post_data_raw)
134+
try:
135+
post_data = json.loads(post_data_raw)
136+
except ValueError as e:
137+
print("Error decoding JSON data:", e)
138+
# Handle the error (e.g., send an error response to the client)
139+
await response.send("Invalid JSON data", status_code=400)
140+
return
134141
request = Request(post_data)
135-
response = Response(writer)
136142
# check if the url is a function route and if so run the function
137143
path_components = self.get_path_components(url)
138144
print("path_components: "+str(path_components))

0 commit comments

Comments
 (0)