Skip to content

Commit

Permalink
fix: Error: fix 500 server error
Browse files Browse the repository at this point in the history
  • Loading branch information
levos-snr committed Aug 30, 2024
1 parent d339de4 commit 1694fdf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 116 deletions.
40 changes: 0 additions & 40 deletions api/__init__.py

This file was deleted.

22 changes: 22 additions & 0 deletions api/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_migrate import Migrate
import os




# init
app = Flask(__name__)
CORS(app)


# Database configuration
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('POSTGRES_URL')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False


#instance of db
db = SQLAlchemy(app)
migrate = Migrate(app, db)
79 changes: 31 additions & 48 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,47 @@
from flask import request, jsonify
from . import app, db
from .models import User
from flask import request, jsonify
from config import app, db
from models import User
import traceback



@app.route("/api/welcome")
def hello_world():
return jsonify({"message": "Welcome to the API"}), 200

@app.route('/api/users', methods=['GET'])
def get_users():
try:
users = User.query.all()
return jsonify([user.to_json() for user in users]), 200
except Exception as e:
app.logger.error(f"Error fetching users: {str(e)}")
return jsonify({"error": "An error occurred while fetching users"}), 500
users = User.query.all()
return jsonify([user.to_json() for user in users]), 200

@app.route('/api/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
try:
user = User.query.get_or_404(user_id)
return jsonify(user.to_json()), 200
except Exception as e:
app.logger.error(f"Error fetching user {user_id}: {str(e)}")
return jsonify({"error": f"An error occurred while fetching user {user_id}"}), 500
user = User.query.get_or_404(user_id)
return jsonify(user.to_json()), 200

@app.route('/api/users/<int:user_id>', methods=['PATCH'])
def update_user(user_id):
try:
data = request.json
user = User.query.get_or_404(user_id)
user.first_name = data.get('first_name', user.first_name)
user.last_name = data.get('last_name', user.last_name)
user.email = data.get('email', user.email)
user.is_active = data.get('is_active', user.is_active)
user.is_admin = data.get('is_admin', user.is_admin)
db.session.commit()
return jsonify(user.to_json()), 200
except Exception as e:
db.session.rollback()
app.logger.error(f"Error updating user {user_id}: {str(e)}")
return jsonify({"error": f"An error occurred while updating user {user_id}"}), 500

data = request.json
user = User.query.get_or_404(user_id)
user.first_name = data['first_name']
user.last_name = data['last_name']
user.email = data['email']
user.is_active = data['is_active']
user.is_admin = data['is_admin']
db.session.commit()
return jsonify(user.to_json()), 200
@app.route('/api/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
try:
user = User.query.get_or_404(user_id)
db.session.delete(user)
db.session.commit()
return jsonify({"message": "User deleted"}), 200
except Exception as e:
db.session.rollback()
app.logger.error(f"Error deleting user {user_id}: {str(e)}")
return jsonify({"error": f"An error occurred while deleting user {user_id}"}), 500

# Error handling for 404 and 500 errors
@app.errorhandler(404)
def not_found_error(error):
return jsonify({"error": "Not found"}), 404
user = User.query.get_or_404(user_id)
db.session.delete(user)
db.session.commit()
return jsonify({"message": "User deleted"}), 200

@app.errorhandler(Exception)
def handle_exception(e):
# Log the full traceback
app.logger.error(f"Unhandled Exception: {str(e)}\n{traceback.format_exc()}")
return jsonify(error=str(e)), 500

@app.errorhandler(500)
def internal_error(error):
db.session.rollback()
return jsonify({"error": "Internal server error"}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
8 changes: 0 additions & 8 deletions api/vercel.json

This file was deleted.

28 changes: 8 additions & 20 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
{
"builds": [
{
"src": "api/__init__.py",
"use": "@vercel/python",
"config": {
"maxLambdaSize": "15mb",
"runtime": "python3.9"
}
},
{
"src": "package.json",
"use": "@vercel/next"
}
],
"builds": [
{ "src": "api/main.py", "use": "@vercel/python", "config": { "runtime": "python3.12" } },
{ "src": "package.json", "use": "@vercel/next" }
],
"routes": [
{ "src": "/api/(.*)", "dest": "api/__init__.py" },
{ "src": "/api/(.*)", "dest": "api/main.py" },
{ "src": "/(.*)", "dest": "/$1" }
],
"env": {
"FLASK_ENV": "production"
}
}
]
}

0 comments on commit 1694fdf

Please sign in to comment.