From 0c6a230ff2eaa7903a687879d8ef3651a2cdf093 Mon Sep 17 00:00:00 2001 From: Kevin Reynolds Date: Mon, 6 May 2024 12:10:19 -0400 Subject: [PATCH] better error messages --- labapp/app/app.py | 67 ++++++++++++++++++++++++++++++--------------- labapp/app/fetch.py | 6 ++-- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/labapp/app/app.py b/labapp/app/app.py index ebc831c..75c216d 100644 --- a/labapp/app/app.py +++ b/labapp/app/app.py @@ -5,6 +5,7 @@ import re import json import requests +from requests.exceptions import HTTPError import base64 import urllib from flask import Flask, render_template, jsonify, request, redirect, make_response, flash, url_for @@ -272,8 +273,10 @@ def ex_test(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_test2') def ex_test2(): @@ -284,8 +287,10 @@ def ex_test2(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_lb1') @@ -300,8 +305,10 @@ def lb_aws(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_lb2') def lb_azure(): @@ -320,8 +327,10 @@ def lb_azure(): raise ValueError("Failed to find Azure Origin Pool") except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_route1') def route1(): @@ -342,8 +351,10 @@ def route1(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_route2') def route2(): @@ -366,8 +377,10 @@ def route2(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_manip1') def manip1(): @@ -382,8 +395,10 @@ def manip1(): return jsonify(status='success', data=r_data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_manip2') def manip2(): @@ -400,8 +415,10 @@ def manip2(): return jsonify(status='success', data=r_data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_manip3') def manip3(): @@ -424,8 +441,10 @@ def manip3(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_port1') def port1(): @@ -439,8 +458,10 @@ def port1(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") @app.route('/_port2', methods=['POST']) def port2(): @@ -453,8 +474,10 @@ def port2(): return jsonify(status='success', data=data) except (LabException, ValueError) as e: return jsonify(status='fail', error=str(e)) - except requests.RequestException: - return jsonify(status='fail', error="Connection/Request Error") + except HTTPError as e: + return jsonify(status='fail', error=f"HTTP Error: {e.response.status_code} {e.response.reason}") + except requests.RequestException as e: + return jsonify(status='fail', error=f"Error: Connection/Network Issue") if __name__ == '__main__': diff --git a/labapp/app/fetch.py b/labapp/app/fetch.py index 9d0013a..a0258f2 100644 --- a/labapp/app/fetch.py +++ b/labapp/app/fetch.py @@ -43,7 +43,7 @@ def cloudapp_fetch(session, url, timeout, prop, value): response.raise_for_status() data = response.json() if data.get(prop) != value: - raise ValueError(f'Invalid {prop}: expected {value}, got {data.get(prop)}') + raise ValueError(f'Value Error for {prop}: expected {value}, got {data.get(prop)}') if data.get("request_headers"): clean_headers = headers_cleaner(data['request_headers']) data['request_headers'] = clean_headers @@ -61,7 +61,7 @@ def cloudapp_req_headers(session, url, timeout, headers): for header in headers: head_value = req_headers.get(header) if not head_value: - raise ValueError(f"Header {header} not found request headers") + raise ValueError(f"Header Error: {header} not found request headers") clean_headers = headers_cleaner(data['request_headers']) data['request_headers'] = clean_headers return data @@ -77,6 +77,6 @@ def cloudapp_res_headers(session, url, timeout, headers): for header in headers: head_value = data.get(header) if not head_value: - raise ValueError(f"Header {header} not found response headers from {url}") + raise ValueError(f"Header Error: {header} not found response headers from {url}") header_dict = dict(data) return header_dict