Skip to content

Bug: Duplicate Email Check in User Registration #1336

@iRahmanG

Description

@iRahmanG

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions