From 42c71fa8449c25c7663b29548433d105ff979b3f Mon Sep 17 00:00:00 2001 From: James Kirk <james.kirk@noc.ac.uk> Date: Wed, 1 Mar 2023 14:44:38 +0000 Subject: [PATCH] fix: correct error code resp refactor: better err msgs for invalid JSON --- endpoints/client.py | 25 +++++++++++++++++-------- endpoints/notify.py | 7 ++++++- endpoints/send.py | 7 ++++++- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/endpoints/client.py b/endpoints/client.py index 60fca56..c22df55 100644 --- a/endpoints/client.py +++ b/endpoints/client.py @@ -1,11 +1,13 @@ -from flask_restful import Resource, request, abort +import logging + +from flask_restful import Resource, abort, request from marshmallow import Schema, fields -import json -import os -import random -import string +from werkzeug.exceptions import BadRequest + from models.client_model import ClientModel +logging.basicConfig(level=logging.INFO) +logging.getLogger("pika").setLevel(logging.ERROR) class ClientSchema(Schema): client_id = fields.Str(required=True) @@ -37,7 +39,11 @@ class Client(Resource): return client, 204 def put(self, client_id): - args = request.get_json() + try: + args = request.get_json() + except BadRequest: + return "POSTed body is invalid JSON", 400 + errors = self.schema.validate(args) if errors: abort(400, message=str(errors)) @@ -63,7 +69,10 @@ class ClientList(Resource): } def post(self): - args = request.get_json() + try: + args = request.get_json() + except BadRequest: + return "POSTed body is invalid JSON", 400 errors = self.schema.validate(args) if errors: @@ -71,7 +80,7 @@ class ClientList(Resource): client = self.clients_file.find(args["client_id"]) if client: - abort(403, message="Duplicate client id: {}".format(client_id)) + abort(409, message="Duplicate client id: {}".format(args["client_id"])) else: client = self.clients_file.add(args) return client, 201 diff --git a/endpoints/notify.py b/endpoints/notify.py index dff460e..3035e79 100644 --- a/endpoints/notify.py +++ b/endpoints/notify.py @@ -2,6 +2,7 @@ import json from flask_restful import abort, request from marshmallow import Schema, fields +from werkzeug.exceptions import BadRequest from endpoints.auth_resource import AuthResource from rmq import write_to_queue @@ -20,7 +21,11 @@ class Notify(AuthResource): self.schema = NotifySchema() def post(self): - args = request.get_json() + try: + args = request.get_json() + except BadRequest: + return "POSTed body is invalid JSON", 400 + errors = self.schema.validate(args) if errors: abort(400, message=str(errors)) diff --git a/endpoints/send.py b/endpoints/send.py index cb64c4a..edb5277 100644 --- a/endpoints/send.py +++ b/endpoints/send.py @@ -2,6 +2,7 @@ import json from flask_restful import abort, request from marshmallow import Schema, fields +from werkzeug.exceptions import BadRequest from endpoints.auth_resource import AuthResource from rmq import write_to_queue @@ -21,7 +22,11 @@ class Send(AuthResource): self.schema = SendSchema() def post(self): - args = request.get_json() + try: + args = request.get_json() + except BadRequest: + return "POSTed body is invalid JSON", 400 + errors = self.schema.validate(args) if errors: abort(400, message=str(errors)) -- GitLab