Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jonfairbanks/bad-app
A Flask app with incorrect HTTP error code logging (testing only!)
https://github.com/jonfairbanks/bad-app
error-handling flask http-error-handling
Last synced: 6 days ago
JSON representation
A Flask app with incorrect HTTP error code logging (testing only!)
- Host: GitHub
- URL: https://github.com/jonfairbanks/bad-app
- Owner: jonfairbanks
- Created: 2024-02-25T04:02:39.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-02-25T04:26:05.000Z (10 months ago)
- Last Synced: 2024-11-07T17:53:38.102Z (about 2 months ago)
- Topics: error-handling, flask, http-error-handling
- Language: Dockerfile
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bad App
Remember to log your HTTP codes correctly! 😊
A sample Flask application that implements two endpoints:
- `/`: A simple Hello World endpoint which returns an HTTP 200 as expected
- `/error`: An endpoint with logic errors and incorrect logging of HTTP error codes, resulting in HTTP 200 even in the event of an errorThis is a demonstration of how HTTP error codes can incorrectly be implemented in an application, which can mask errors and hide issues in observability tools.
Incorrect HTTP 200 returned (implemented in this app):
```
@app.route("/error", methods=["GET"])
def error():
try:
val1 = "string"
val2 = 100
final_val = val1 + val2 # This will fail -- you can't concatenate a string with an integer!
return final_val
except Exception as e:
logger.error(f"Error: {e}")
return jsonify({"error": str(e)}) # This should return an HTTP 500 as there was an error; but instead will return with HTTP 200!
```Correct HTTP 500 returned:
```
@app.route("/error", methods=["GET"])
def error():
try:
val1 = "string"
val2 = 100
final_val = val1 + val2 # This will fail -- you can't concatenate a string with an integer!
return final_val
except Exception as e:
logger.error(f"Error: {e}")
return jsonify({"error": str(e)}), 500 # This now correctly returns HTTP 500!
```Alternatively, you can use Flask's built-in abort method like `abort(500)` to throw the proper status code.
### Setup
Local:
```
pip install pipenv
pipenv install && pipenv shell
pipenv run python main.py
```Docker:
```
docker run -d -p 5000:5000 jonfairbanks/bad-app
```