Skip to content

Commit 107f16e

Browse files
author
Vianpyro
committed
Changed approach to jsonify
1 parent e47d02f commit 107f16e

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

error_handlers.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
from werkzeug.exceptions import NotFound, BadRequest, Unauthorized, Forbidden, MethodNotAllowed
44

55
def handle_database_error(error):
6-
current_app.logger.error('Database connection error: %s', error)
7-
return jsonify({'error': 'Database connection error'}), 500
6+
current_app.logger.error(f"Database connection error: {error}")
7+
return jsonify(error="Database connection error"), 500
88

99
def handle_not_found_error(error):
10-
current_app.logger.error('Not found error: %s', error)
11-
return jsonify({'error': 'Resource not found'}), 404
10+
current_app.logger.error(f"Not found error: {error}")
11+
return jsonify(error="Resource not found"), 404
1212

1313
def handle_bad_request_error(error):
14-
current_app.logger.error('Bad request error: %s', error)
15-
return jsonify({'error': 'Bad request'}), 400
14+
current_app.logger.error(f"Bad request error: {error}")
15+
return jsonify(error="Bad request"), 400
1616

1717
def handle_unauthorized_error(error):
18-
current_app.logger.error('Unauthorized error: %s', error)
19-
return jsonify({'error': 'Unauthorized'}), 401
18+
current_app.logger.error(f"Unauthorized error: {error}")
19+
return jsonify(error="Unauthorized"), 401
2020

2121
def handle_forbidden_error(error):
22-
current_app.logger.error('Forbidden error: %s', error)
23-
return jsonify({'error': 'Forbidden'}), 403
22+
current_app.logger.error(f"Forbidden error: {error}")
23+
return jsonify(error="Forbidden"), 403
2424

2525
def handle_method_not_allowed_error(error):
26-
current_app.logger.error('Method not allowed error: %s', error)
27-
return jsonify({'error': 'Method not allowed'}), 405
26+
current_app.logger.error(f"Method not allowed error: {error}")
27+
return jsonify(error="Method not allowed"), 405
2828

2929
def handle_server_error(error):
30-
current_app.logger.error('Server error: %s', error)
31-
return jsonify({'error': 'Internal server error'}), 500
30+
current_app.logger.error(f"Server error: {error}")
31+
return jsonify(error="Internal server error"), 500
3232

3333
def register_error_handlers(app):
3434
app.register_error_handler(DatabaseError, handle_database_error)

routes/authentication.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def register():
3030
password = data.get('password')
3131

3232
if not username or not email or not password:
33-
return jsonify({"message": "Username, email, and password are required"}), 400
33+
return jsonify(message="Username, email, and password are required"), 400
3434

3535
if not validate_password(password):
36-
return jsonify({"message": "Password does not meet security requirements"}), 400
36+
return jsonify(message="Password does not meet security requirements"), 400
3737

3838
salt = os.urandom(16)
3939
hashed_password = hash_password_with_salt_and_pepper(password, salt)
@@ -45,11 +45,11 @@ def register():
4545
db.commit()
4646
except MySQLError as e:
4747
if e.args[0] == 1644:
48-
return jsonify({"message": "Email already in use"}), 400
48+
return jsonify(message="Email already in use"), 400
4949
else:
50-
return jsonify({"message": "An error occurred during registration"}), 500
50+
return jsonify(message="An error occurred during registration"), 500
5151

52-
return jsonify({"message": "User created successfully"}), 201
52+
return jsonify(message="User created successfully"), 201
5353

5454
@authentications_blueprint.route('/login', methods=['POST'])
5555
def login():
@@ -58,7 +58,7 @@ def login():
5858
password = data.get('password')
5959

6060
if not email or not password:
61-
return jsonify({"message": "Email and password are required"}), 400
61+
return jsonify(message="Email and password are required"), 400
6262

6363
db = get_db_connection()
6464
with db.cursor() as cursor:
@@ -78,7 +78,7 @@ def login():
7878
except VerifyMismatchError:
7979
pass
8080

81-
return jsonify({"message": "Invalid credentials"}), 401
81+
return jsonify(message="Invalid credentials"), 401
8282

8383
@authentications_blueprint.route('/protected', methods=['GET'])
8484
@jwt_required()

0 commit comments

Comments
 (0)