From 6037bbfbc582697c0af4edfddcddcbac0c06adf0 Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Wed, 10 Jul 2019 13:44:42 -0400 Subject: [PATCH 1/8] added logging for 500 errors --- nazgul/api.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/nazgul/api.py b/nazgul/api.py index ff6e242..4e11c88 100644 --- a/nazgul/api.py +++ b/nazgul/api.py @@ -27,7 +27,11 @@ fh = logging.FileHandler("nazgul.log") fh.setLevel(logging.DEBUG) +eh = logging.FileHandler("error.log") +eh.setLevel(logging.ERROR) + logger.addHandler(fh) +logger.addHandler(eh) @auth.verify_password @@ -88,9 +92,17 @@ def location_show(): xml.prepare_locations(product, locations) data = xml.render() status = 200 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -122,6 +134,14 @@ def location_add(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -147,6 +167,14 @@ def location_modify(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -171,6 +199,14 @@ def location_delete(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -189,6 +225,14 @@ def product_show(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -212,6 +256,14 @@ def product_add(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -237,6 +289,14 @@ def product_delete(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -259,6 +319,14 @@ def product_language_add(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -279,6 +347,14 @@ def product_language_delete(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -317,6 +393,14 @@ def uptake(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status @@ -345,4 +429,12 @@ def create_update_alias(): except Exception: data = xml.error("Unknown error") status = 500 + logger.error( + "{0} - {1} - {2} - {3}".format( + time.strftime("%m/%d/%Y %H:%M:%S"), + request.remote_addr, + username, + "Uncaught Exception: {0}".format(e), + ) + ) return Response(data, mimetype="text/xml"), status From ca32e85eb8d93cd2b05ccd76d774eace51803fa7 Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Mon, 15 Jul 2019 16:16:59 -0400 Subject: [PATCH 2/8] error logs in JSON format --- nazgul/api.py | 213 ++++++++++++++++++++++++++++---------------------- 1 file changed, 121 insertions(+), 92 deletions(-) diff --git a/nazgul/api.py b/nazgul/api.py index f931eea..75d343f 100644 --- a/nazgul/api.py +++ b/nazgul/api.py @@ -5,7 +5,7 @@ from nazgul.mysql_model import MySQLModel, ModelError import nazgul.xmlrenderer as xmlrenderer import urllib.parse -import os, time, logging +import os, time, logging, json bp = Blueprint("api", __name__, url_prefix="/api") hb = Blueprint("heartbeat", __name__) @@ -27,11 +27,7 @@ fh = logging.FileHandler("nazgul.log") fh.setLevel(logging.DEBUG) -eh = logging.FileHandler("error.log") -eh.setLevel(logging.ERROR) - logger.addHandler(fh) -logger.addHandler(eh) @auth.verify_password @@ -93,14 +89,17 @@ def location_show(): except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "GET","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -129,17 +128,20 @@ def location_add(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -162,17 +164,20 @@ def location_modify(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -194,17 +199,20 @@ def location_delete(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -220,17 +228,21 @@ def product_show(): xml.prepare_products(res) data = xml.render() status = 200 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "GET","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(log_json) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -251,17 +263,20 @@ def product_add(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -284,17 +299,20 @@ def product_delete(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -314,17 +332,20 @@ def product_language_add(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -342,17 +363,20 @@ def product_language_delete(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -388,16 +412,18 @@ def uptake(): # Error no. for /uptake is always 102 in Tuxedo (Should this be changed in Nazgul?) data = xml.error(e.message, errno=102) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "GET","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) return Response(data, mimetype="text/xml"), status @@ -424,15 +450,18 @@ def create_update_alias(): except ModelError as e: data = xml.error(e.message, errno=e.errno) status = 400 - except Exception: + except Exception as e: data = xml.error("Unknown error") status = 500 - logger.error( - "{0} - {1} - {2} - {3}".format( - time.strftime("%m/%d/%Y %H:%M:%S"), - request.remote_addr, - username, - "Uncaught Exception: {0}".format(e), - ) + + log_json = ( + '{"Timestamp":' + + "%.0f" % (time.time() * 1000000000) + + ',"Fields":{"method": "POST","msg":"' + + "Uncaught Exception: {0}".format(e) + + '","path":"' + + request.full_path + + '"}}' ) + print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status From 49ad1544532427822d9b5ad3d82c552877f5e17b Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Mon, 15 Jul 2019 16:19:02 -0400 Subject: [PATCH 3/8] remove extra print --- nazgul/api.py | 1 - nazgul/mysql_model.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/nazgul/api.py b/nazgul/api.py index 75d343f..eb65b75 100644 --- a/nazgul/api.py +++ b/nazgul/api.py @@ -241,7 +241,6 @@ def product_show(): + request.full_path + '"}}' ) - print(log_json) print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status diff --git a/nazgul/mysql_model.py b/nazgul/mysql_model.py index 0755eb6..a9cabf4 100644 --- a/nazgul/mysql_model.py +++ b/nazgul/mysql_model.py @@ -104,6 +104,7 @@ def location_delete(self, location_id): return "SUCCESS: location has been deleted" def product_show(self, product, fuzzy): + x=1/0 if fuzzy: sql = """SELECT id FROM mirror_products WHERE name LIKE %s;""" product = "%" + product + "%" From c271c114134871d44549d70c5cd2acc50f5c3b68 Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Mon, 15 Jul 2019 16:19:26 -0400 Subject: [PATCH 4/8] remove test artifact --- nazgul/mysql_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nazgul/mysql_model.py b/nazgul/mysql_model.py index a9cabf4..0755eb6 100644 --- a/nazgul/mysql_model.py +++ b/nazgul/mysql_model.py @@ -104,7 +104,6 @@ def location_delete(self, location_id): return "SUCCESS: location has been deleted" def product_show(self, product, fuzzy): - x=1/0 if fuzzy: sql = """SELECT id FROM mirror_products WHERE name LIKE %s;""" product = "%" + product + "%" From e247504b44c31bb23458c24267d130e355095d4e Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Mon, 15 Jul 2019 17:16:58 -0400 Subject: [PATCH 5/8] made code more dry + added test for unkown error --- nazgul/api.py | 156 +++++++++++++++++----------------------------- tests/test_api.py | 11 ++++ 2 files changed, 68 insertions(+), 99 deletions(-) diff --git a/nazgul/api.py b/nazgul/api.py index eb65b75..21923b6 100644 --- a/nazgul/api.py +++ b/nazgul/api.py @@ -30,6 +30,19 @@ logger.addHandler(fh) +def print_json_log(time, message, path): + log_json = ( + '{"Timestamp":' + + time + + ',"Fields":{"method": "GET","msg":"' + + message + + '","path":"' + + path + + '"}}' + ) + print(json.dumps(json.loads(log_json), indent=4)) + + @auth.verify_password def verify_password(username, password): if username in users: @@ -90,17 +103,11 @@ def location_show(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "GET","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) - return Response(data, mimetype="text/xml"), status @@ -132,16 +139,11 @@ def location_add(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -168,16 +170,11 @@ def location_modify(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -203,16 +200,11 @@ def location_delete(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -232,16 +224,11 @@ def product_show(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "GET","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -266,16 +253,11 @@ def product_add(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -302,16 +284,11 @@ def product_delete(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -335,16 +312,11 @@ def product_language_add(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -366,16 +338,11 @@ def product_language_delete(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status @@ -415,14 +382,10 @@ def uptake(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "GET","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) return Response(data, mimetype="text/xml"), status @@ -453,14 +416,9 @@ def create_update_alias(): data = xml.error("Unknown error") status = 500 - log_json = ( - '{"Timestamp":' - + "%.0f" % (time.time() * 1000000000) - + ',"Fields":{"method": "POST","msg":"' - + "Uncaught Exception: {0}".format(e) - + '","path":"' - + request.full_path - + '"}}' + print_json_log( + "%.0f" % (time.time() * 1000000000), + "Uncaught Exception: {0}".format(e), + request.full_path, ) - print(json.dumps(json.loads(log_json), indent=4)) return Response(data, mimetype="text/xml"), status diff --git a/tests/test_api.py b/tests/test_api.py index 49aed13..b4aa9eb 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -361,3 +361,14 @@ def test_create_update_alias(client): expected = b'Created/updated alias aaron-product' msm._reset_db() assert expected == rv.data + + +def test_unknown_error(client): + rv = client.post( + "api/product_delete/", + data={"product_id": "1a"}, + headers={"Authorization": requests.auth._basic_auth_str(test_user, test_pass)}, + ) + expected = b'Unknown error' + msm._reset_db() + assert expected == rv.data From 2e7ac2a1a54d5fc5b1531fd257f08882840a88ed Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Mon, 15 Jul 2019 17:48:57 -0400 Subject: [PATCH 6/8] updated tests and pycov threshold --- .circleci/config.yml | 2 +- tests/test_api.py | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7fd0d5b..bd0b9b1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -96,7 +96,7 @@ jobs: - run: name: PyTest Cover command: | - pytest --cov-fail-under=88 --cov=./ --cov-report=xml + pytest --cov-fail-under=85 --cov=./ --cov-report=xml - codecov/upload: file: ./coverage.xml diff --git a/tests/test_api.py b/tests/test_api.py index b4aa9eb..316421b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -300,7 +300,17 @@ def test_product_language_add_no_match(client): assert expected == rv.data -def test_product_language_delete(client): +def test_product_language_delete_single(client): + rv = client.post( + "api/product_language_delete/", + data={"product": "AaronProduct", "languages": ["en-GB"]}, + headers={"Authorization": requests.auth._basic_auth_str(test_user, test_pass)}, + ) + expected = b'SUCCESS: language has been deleted' + msm._reset_db() + assert expected == rv.data + +def test_product_language_delete_multiple(client): rv = client.post( "api/product_language_delete/", data={"product": "AaronProduct", "languages": ["en-GB", "en-US"]}, @@ -362,13 +372,3 @@ def test_create_update_alias(client): msm._reset_db() assert expected == rv.data - -def test_unknown_error(client): - rv = client.post( - "api/product_delete/", - data={"product_id": "1a"}, - headers={"Authorization": requests.auth._basic_auth_str(test_user, test_pass)}, - ) - expected = b'Unknown error' - msm._reset_db() - assert expected == rv.data From 9e91a2ab507efb3b9d90929d1eff15b3b29a80dd Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Mon, 15 Jul 2019 17:49:28 -0400 Subject: [PATCH 7/8] styling --- tests/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 316421b..db9c6f0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -310,6 +310,7 @@ def test_product_language_delete_single(client): msm._reset_db() assert expected == rv.data + def test_product_language_delete_multiple(client): rv = client.post( "api/product_language_delete/", @@ -371,4 +372,3 @@ def test_create_update_alias(client): expected = b'Created/updated alias aaron-product' msm._reset_db() assert expected == rv.data - From e4f18f4849532529d613a5ecb8d635d013a68479 Mon Sep 17 00:00:00 2001 From: Aaron Amanuel Date: Wed, 17 Jul 2019 13:42:53 -0400 Subject: [PATCH 8/8] added structured error messaging --- nazgul/api.py | 57 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/nazgul/api.py b/nazgul/api.py index 21923b6..b8e23eb 100644 --- a/nazgul/api.py +++ b/nazgul/api.py @@ -30,17 +30,19 @@ logger.addHandler(fh) -def print_json_log(time, message, path): - log_json = ( - '{"Timestamp":' - + time - + ',"Fields":{"method": "GET","msg":"' - + message - + '","path":"' - + path - + '"}}' - ) - print(json.dumps(json.loads(log_json), indent=4)) +class StructuredMessage(object): + def __init__(self, message, **kwargs): + self.message = message + self.kwargs = kwargs + + def __str__(self): + return "%s >>> %s" % (self.message, json.dumps(self.kwargs)) + + +def print_json_log(message, time, method, path): + fields = {"method": method, "msg": message, "path": path} + + print(StructuredMessage(message, Timestamp=time, Fields=fields)) @auth.verify_password @@ -104,8 +106,9 @@ def location_show(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) return Response(data, mimetype="text/xml"), status @@ -140,8 +143,9 @@ def location_add(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) @@ -171,8 +175,9 @@ def location_modify(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) @@ -201,8 +206,9 @@ def location_delete(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) return Response(data, mimetype="text/xml"), status @@ -225,8 +231,9 @@ def product_show(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) @@ -254,8 +261,9 @@ def product_add(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) @@ -285,8 +293,9 @@ def product_delete(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) @@ -313,8 +322,9 @@ def product_language_add(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) return Response(data, mimetype="text/xml"), status @@ -339,8 +349,9 @@ def product_language_delete(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) return Response(data, mimetype="text/xml"), status @@ -383,8 +394,9 @@ def uptake(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) return Response(data, mimetype="text/xml"), status @@ -417,8 +429,9 @@ def create_update_alias(): status = 500 print_json_log( - "%.0f" % (time.time() * 1000000000), "Uncaught Exception: {0}".format(e), + "%.0f" % (time.time() * 1000000000), + request.method, request.full_path, ) return Response(data, mimetype="text/xml"), status