Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kreynoldsf5 committed May 6, 2024
1 parent 806fbde commit 0c6a230
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
67 changes: 45 additions & 22 deletions labapp/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand All @@ -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')
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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__':
Expand Down
6 changes: 3 additions & 3 deletions labapp/app/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 0c6a230

Please sign in to comment.