Overview
The Chat Server API returns structured error responses when requests fail. All errors follow a consistent format using theErrorResponse DTO structure.
Error response structure
A human-readable error message describing what went wrong
Example error response
HTTP status codes
The API uses the following HTTP status codes to indicate the type of error:400 Bad Request
Returned when the request is malformed or contains invalid data. Common causes include:- Invalid username or password format
- Username or password validation failures
- Attempting to perform an invalid relationship operation
- Relationship does not exist
401 Unauthorized
Returned when authentication fails or is required but not provided:- Authorization token was not provided
- Authorization token is invalid or expired
- Password confirmation incorrect (for sensitive operations like account deletion)
404 Not Found
Returned when a requested resource does not exist.409 Conflict
Returned when the request conflicts with the current state of the server:- Username already exists during registration
Exception types
The following exception types are handled by the API, organized by controller:User exceptions
UserAlreadyExistsException
UserAlreadyExistsException
Status Code: 409 ConflictThrown when: A user attempts to register with a username that already existsExample message:
PasswordTooShortException
PasswordTooShortException
Status Code: 400 Bad RequestThrown when: A password is shorter than the minimum required length (6 characters)Example message:
UsernameTooShortException
UsernameTooShortException
Status Code: 400 Bad RequestThrown when: A username is shorter than the minimum required length (2 characters)Example message:
UsernameTooLongException
UsernameTooLongException
Status Code: 400 Bad RequestThrown when: A username exceeds the maximum allowed length (8 characters)Example message:
UsernameOrPasswordIncorrectException
UsernameOrPasswordIncorrectException
Status Code: 400 Bad RequestThrown when: Login credentials are incorrectExample message:
UnauthorizedException
UnauthorizedException
PasswordIncorrectForUserDeletionException
PasswordIncorrectForUserDeletionException
Status Code: 401 UnauthorizedThrown when: Password confirmation fails during account deletionExample message:
Relationship exceptions
InvalidFriendRequestException
InvalidFriendRequestException
Status Code: 400 Bad RequestThrown when: Attempting to send a friend request to a user that doesn’t existExample message:
SelfFriendException
SelfFriendException
Status Code: 400 Bad RequestThrown when: Attempting to send a friend request to yourselfExample message:
ExistingRelationshipException
ExistingRelationshipException
Status Code: 400 Bad RequestThrown when: Attempting to create a relationship with a user you’re already friends withExample message:
OutgoingRequestAlreadyExistsException
OutgoingRequestAlreadyExistsException
Status Code: 400 Bad RequestThrown when: Attempting to send another friend request when one is already pendingExample message:
RelationshipDoesNotExistException
RelationshipDoesNotExistException
Status Code: 400 Bad RequestThrown when: Attempting to delete or modify a relationship that doesn’t existExample message:
Authentication exceptions
AuthenticationException
AuthenticationException
Status Code: VariesThrown when: Spring Security authentication failsHandled by:
GlobalExceptionController (UserController.java:167-171, UserController.java:179-183, RelationshipController.java:168-172, etc.)This is a generic Spring Security exception that covers various authentication failures. Specific error messages depend on the type of authentication failure.Error handling locations
Error handlers are implemented in the following controller files:- GlobalExceptionController.java - Handles global
AuthenticationExceptionfrom Spring Security - UserController.java - Handles user-related exceptions (registration, login, account management)
- RelationshipController.java - Handles relationship-related exceptions (friend requests, relationship management)
Best practices
When handling errors in your client application:- Always check the HTTP status code first to determine the type of error
- Display the
messagefield to users for human-readable error information - Implement appropriate retry logic for 5xx errors (server errors)
- For 401 errors, redirect users to login or refresh authentication tokens
- For 400 errors, validate user input on the client side to prevent unnecessary API calls