Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions controllers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ async def create_user(user_data: UserRegister):
# Hash the password
hashed_password = bcrypt.hashpw(user_data.password.encode('utf-8'), bcrypt.gensalt())

try:
# Create a new user
user = await db.user.create(
data={
'email': user_data.email,
'name': user_data.name,
'password': hashed_password.decode('utf-8'),
}
)
try:
# Create a new user
user = await db.user.create(
data={
'email': user_data.email,
'name': user_data.name,
'password': hashed_password.decode('utf-8'),
}
)
Comment on lines +59 to +67
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem

The original code lacked specific error handling for the db.user.create operation. Any exceptions raised during user creation would propagate up, potentially leading to unhandled exceptions and generic 500 errors without specific details.

Suggested Fix

Suggested change
try:
# Create a new user
user = await db.user.create(
data={
'email': user_data.email,
'name': user_data.name,
'password': hashed_password.decode('utf-8'),
}
)
try:
# Create a new user
user = await db.user.create(
data={
'email': user_data.email,
'name': user_data.name,
'password': hashed_password.decode('utf-8'),
}
)
Suggested change
try:
# Create a new user
user = await db.user.create(
data={
'email': user_data.email,
'name': user_data.name,
'password': hashed_password.decode('utf-8'),
}
)
except Exception as e:
print(f"Error creating user: {e}")
raise HTTPException(status_code=500, detail="Failed to create user")

Explanation

The added try...except block specifically catches exceptions that may occur during the db.user.create operation. If an exception is caught:

  1. The exception is logged using print(f"Error creating user: {e}").
  2. An HTTPException is raised with a 500 status code and a user-friendly error message, providing more context to the client about the failure.

Additional Context

This change improves the robustness of the user registration process by providing better error handling and more informative error messages.

📍 This suggestion applies to lines 59-67

except Exception as e:
print(f"Error creating user: {e}")
raise HTTPException(status_code=500, detail="Failed to create user")

# Create default job statuses
for status in DEFAULT_STATUSES:
Expand Down