Description
Duplicate emails are currently caught only when the database throws
DataIntegrityViolationException. This makes the code harder to read,
maintain, and extend.
Current Behavior
- User registers with an existing email.
- Save attempt triggers a database constraint error.
- Controller catches the error and returns a generic response.
Expected Behavior
- Service checks if email already exists before saving.
- If duplicate, throw a custom
UserAlreadyExistsException.
- Global exception handler returns a clear, consistent API response.
Proposed Fix
- Add email check in
AuthService.
- Create
UserAlreadyExistsException.
- Handle it in
GlobalExceptionHandler.
- Remove reliance on database errors for business logic.
Benefits
- Cleaner validation in service layer
- Consistent error handling
- Easier to maintain and extend
Files Affected
- AuthService.java
- AuthController.java
- UserRepository.java
- GlobalExceptionHandler.java
- UserAlreadyExistsException.java (new)
Description
Duplicate emails are currently caught only when the database throws
DataIntegrityViolationException. This makes the code harder to read,maintain, and extend.
Current Behavior
Expected Behavior
UserAlreadyExistsException.Proposed Fix
AuthService.UserAlreadyExistsException.GlobalExceptionHandler.Benefits
Files Affected