Giter VIP home page Giter VIP logo

bad-app's Introduction

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 error

This 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

bad-app's People

Contributors

jonfairbanks avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.