Skip to content

Internal Server Error at Root Route /  #2

Open
@ericBlack1

Description

@ericBlack1

Description:
Accessing the root route / triggers an internal server error (HTTP 500) due to an intentional exception in the code.

Steps to Reproduce:

  1. Run the Flask application using the provided code snippet.
  2. Open a web browser and navigate to http://127.0.0.1:5000/.
  3. Observe the error message and stack trace in the browser or console.

Expected Behavior:
The root route / should display the message "Welcome to your first Flask application!" or handle the exception gracefully.

Actual Behavior:
An internal server error (HTTP 500) occurs, with the following error message:

Internal Server Error: /  
Traceback (most recent call last):  
  File "<file_path>", line <line_number>, in home  
    raise Exception("Internal server error.")  
Exception: Internal server error.  

Code Snippet:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    raise Exception("Internal server error.")
    return "Welcome to your first Flask application!"

if __name__ == '__main__':
    app.run(debug=True)

Development Environment:

  • OS: Ubuntu 24.04.
  • Python Version: 3.12.3
  • Flask Version: 3.0.3

Suggested Fix:
Handle the exception gracefully or remove the exception to ensure the route works as expected. Example fix:

@app.route('/')
def home():
    try:
        # Logic here
        return "Welcome to your first Flask application!"
    except Exception as e:
        return f"An error occurred: {str(e)}", 500

Let me know if further details are needed!

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions