Skip to content

Commit

Permalink
feature/error_handling (#16)
Browse files Browse the repository at this point in the history
💯
  • Loading branch information
vantage-ola authored Dec 25, 2023
1 parent 959f6df commit 4a00a2c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
from decouple import config
from models import Car, Track, Laptime ,Driver
from bson import ObjectId

from error_handle import *

app = Flask(__name__)


#error_handling
app.register_error_handler(400, handle_bad_request)
app.register_error_handler(401, handle_unauthorized)
app.register_error_handler(403, handle_forbidden)
app.register_error_handler(404, handle_not_found)
app.register_error_handler(405, handle_method_not_allowed)
app.register_error_handler(500, handle_internal_server_error)
app.register_error_handler(503, handle_service_unavailable)

#mongo db config
MONGO_USERNAME = config('MONGO_USERNAME')
Expand All @@ -17,7 +25,7 @@
db = client['track_now'] #Track Now DB


# missing error handling* (404, 500, 200...)


#`api/collections` routes GET&POST all entries`
@app.route('/api/cars', methods=['GET', 'POST'])
Expand Down
22 changes: 22 additions & 0 deletions backend/error_handle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import jsonify

def handle_bad_request(error):
return jsonify({'error': 'Bad Request', 'message': str(error)}), 400

def handle_unauthorized(error):
return jsonify({'error': 'Unauthorized', 'message': str(error)}), 401

def handle_forbidden(error):
return jsonify({'error': 'Forbidden', 'message': str(error)}), 403

def handle_not_found(error):
return jsonify({'error': 'Not Found', 'message': str(error)}), 404

def handle_method_not_allowed(error):
return jsonify({'error': 'Method Not Allowed', 'message': str(error)}), 405

def handle_internal_server_error(error):
return jsonify({'error': 'Internal Server Error', 'message': str(error)}), 500

def handle_service_unavailable(error):
return jsonify({'error': 'Service Unavailable', 'message': str(error)}), 503

0 comments on commit 4a00a2c

Please sign in to comment.