Skip to content

Refactor: Remove brittle, string-based exception checking in AuthService #16

@fred-maina

Description

@fred-maina

Description

In AuthService.signUp, database errors are caught and handled by checking the exception message for specific constraint names:

} catch (DataIntegrityViolationException e) {
    if (e.getMessage().contains("users_email_key")) {
        authResponse.setMessage("Email already exists");
    } else if (e.getMessage().contains("idx_user_username") || e.getMessage().contains("users_username_key")) {
        authResponse.setMessage("Username already exists");
    } //...
}

This is extremely brittle. If the database schema changes, a constraint is renamed, or a different database driver is used, these checks will fail silently, and the user will get a generic "Data integrity violation" message.

Acceptance Criteria

  1. Remove all e.getMessage().contains(...) checks for database constraints in AuthService.java.
  2. Refactor the signUp method to be more proactive. Before attempting to save, perform explicit checks using the repository:
    • if (userRepository.existsByEmail(request.getEmail())) { ... }
    • if (userRepository.existsByUsernameIgnoreCase(request.getUsername())) { ... }
  3. This moves the logic from fragile, reactive exception handling to robust, proactive validation.
  4. This logic should be applied to setUsername as well, which already does a better job but can be cleaned up.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions