-
Notifications
You must be signed in to change notification settings - Fork 4
CHAT-53: Fixes to improve error messages from cloudflare. #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BravinR
wants to merge
1
commit into
master
Choose a base branch
from
feature-CHAT-53-Fixes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
backend-service/src/main/java/co/teamsphere/api/DTO/ChatSummaryDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,35 @@ | ||
| package co.teamsphere.api.DTO; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.UUID; | ||
|
|
||
| @Data | ||
| @Builder | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class ChatSummaryDTO { | ||
| private UUID id; | ||
| private String chatName; | ||
| private String chatImage; | ||
| private UUID createdBy; | ||
| private MessageDTO lastMessage; | ||
|
|
||
| public ChatSummaryDTO(UUID id, String chatName, String chatImage, UUID createdBy, | ||
| UUID messageId, String content, LocalDateTime timeStamp, | ||
| boolean isRead, UUID userId, UUID chatId, | ||
| String otherUserName, String otherUserProfile) { | ||
| this.id = id; | ||
| this.chatName = chatName; | ||
| this.chatImage = chatImage; | ||
| this.createdBy = createdBy; | ||
| this.lastMessage = (messageId != null) | ||
| ? new MessageDTO(messageId, content, timeStamp, isRead, userId, chatId) | ||
| : null; | ||
| this.chatName = otherUserName; | ||
| this.chatImage = otherUserProfile; | ||
| } | ||
| } | ||
|
|
4 changes: 4 additions & 0 deletions
4
backend-service/src/main/java/co/teamsphere/api/DTO/MessageDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import co.teamsphere.api.request.LoginRequest; | ||
| import co.teamsphere.api.request.SignupRequest; | ||
| import co.teamsphere.api.response.AuthResponse; | ||
| import co.teamsphere.api.response.ErrorResponse; | ||
| import co.teamsphere.api.services.AuthenticationService; | ||
| import co.teamsphere.api.utils.GoogleAuthRequest; | ||
| import co.teamsphere.api.utils.GoogleUserInfo; | ||
|
|
@@ -19,6 +20,7 @@ | |
| import jakarta.validation.Valid; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.util.UUID; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
@@ -93,30 +95,33 @@ public ResponseEntity<String> verifyJwtToken() { | |
| ), | ||
| @ApiResponse(responseCode = "400", description = "Invalid input or user already exists") | ||
| }) | ||
| @PostMapping(value="/signup", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
| public ResponseEntity<AuthResponse> userSignupMethod ( | ||
| @PostMapping(value = "/signup", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
| public ResponseEntity<?> userSignupMethod( | ||
| @Schema(description = "User details", implementation = SignupRequest.class) | ||
| @Valid @ModelAttribute SignupRequest request) throws UserException, ProfileImageException { | ||
| try { | ||
| log.info("Processing signup request for user with email: {}, username:{}", request.getEmail(), request.getUsername()); | ||
| @Valid @ModelAttribute SignupRequest request) { | ||
|
|
||
| AuthResponse authResponse = authenticationService.signupUser(request); | ||
| log.info("Processing signup request for user with email: {}, username: {}", request.getEmail(), request.getUsername()); | ||
|
|
||
| try { | ||
| AuthResponse authResponse = authenticationService.signupUser(request); | ||
| log.info("Signup process completed successfully for user with email: {}", request.getEmail()); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(authResponse); | ||
|
|
||
| return new ResponseEntity<>(authResponse, HttpStatus.CREATED); | ||
| } catch (UserException e) { | ||
| log.error("Error during signup process", e); | ||
| throw e; // Rethrow specific exception to be handled by global exception handler | ||
| } catch (ProfileImageException e){ | ||
| log.warn("File type not accepted, {}", request.getFile().getContentType()); | ||
| throw new ProfileImageException("Profile Picture type is not allowed!"); | ||
| log.error("User-related error during signup process", e); | ||
| return buildErrorResponse(HttpStatus.BAD_REQUEST, e.getMessage(), "/signup"); | ||
|
|
||
| } catch (ProfileImageException e) { | ||
| log.warn("File type not accepted: {}", request.getFile().getContentType()); | ||
| return buildErrorResponse(HttpStatus.UNSUPPORTED_MEDIA_TYPE, "Profile picture type is not allowed!", "/signup"); | ||
|
|
||
| } catch (Exception e) { | ||
| log.error("Unexpected error during signup process", e); | ||
| throw new UserException("Unexpected error during signup process"); | ||
| return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error during signup process", "/signup"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| } | ||
| } | ||
|
|
||
|
|
||
| @Operation(summary = "Login a user", description = "Login with email and password.") | ||
| @ApiResponses(value = { | ||
| @ApiResponse( | ||
|
|
@@ -130,27 +135,29 @@ public ResponseEntity<AuthResponse> userSignupMethod ( | |
| @ApiResponse(responseCode = "401", description = "Invalid credentials") | ||
| }) | ||
| @PostMapping("/login") | ||
| public ResponseEntity<AuthResponse> userLoginMethod( | ||
| public ResponseEntity<?> userLoginMethod( | ||
| @Schema(description = "Login request body", implementation = LoginRequest.class) | ||
| @Valid @RequestBody LoginRequest loginRequest) throws UserException { | ||
| try { | ||
| log.info("Processing login request for user with username: {}", loginRequest.getEmail()); | ||
| @Valid @RequestBody LoginRequest loginRequest) { | ||
|
|
||
| AuthResponse authResponse = authenticationService.loginUser(loginRequest.getEmail(), loginRequest.getPassword()); | ||
| log.info("Processing login request for user with username: {}", loginRequest.getEmail()); | ||
|
|
||
| try { | ||
| AuthResponse authResponse = authenticationService.loginUser(loginRequest.getEmail(), loginRequest.getPassword()); | ||
| log.info("Login successful for user with username: {}", loginRequest.getEmail()); | ||
| return ResponseEntity.ok(authResponse); | ||
|
|
||
| return new ResponseEntity<>(authResponse, HttpStatus.OK); | ||
| } catch (BadCredentialsException e) { | ||
| log.warn("Authentication failed for user with username: {}", loginRequest.getEmail()); | ||
| throw new UserException("Invalid username or password."); | ||
| return buildErrorResponse(HttpStatus.UNAUTHORIZED, "Invalid username or password.", "/login"); | ||
|
|
||
| } catch (Exception e) { | ||
| log.error("Unexpected error during login process", e); | ||
| throw new UserException("Unexpected error during login process."); | ||
| return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error during login process.", "/login"); | ||
| } | ||
| } | ||
|
|
||
| @Transactional // move business logic to service layer | ||
| //TODO: move business logic to service layer | ||
| @Transactional | ||
| @Operation(summary = "Authenticate via Google", description = "login/signup via Google OAuth.") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "200", | ||
|
|
@@ -213,4 +220,18 @@ public ResponseEntity<AuthResponse> authenticateWithGoogleMethod( | |
| return new ResponseEntity<>(new AuthResponse("Error during Google authentication: " + e.getMessage(), false), HttpStatus.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
| private ResponseEntity<ErrorResponse> buildErrorResponse(HttpStatus status, String message, String endpoint) { | ||
| ErrorResponse errorResponse = new ErrorResponse( | ||
| new ErrorResponse.ErrorDetails( | ||
| status.value(), | ||
| message, | ||
| "An error occurred while processing the request.", | ||
| endpoint, | ||
| "POST", | ||
| Instant.now().toString(), | ||
| UUID.randomUUID().toString() // Unique request ID for tracking | ||
| ) | ||
| ); | ||
| return ResponseEntity.status(status).body(errorResponse); | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
backend-service/src/main/java/co/teamsphere/api/exception/CloudflareException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package co.teamsphere.api.exception; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class CloudflareException { | ||
| private int code; | ||
| private String message; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
backend-service/src/main/java/co/teamsphere/api/response/ErrorResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package co.teamsphere.api.response; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @AllArgsConstructor | ||
| public class ErrorResponse { | ||
| private ErrorDetails error; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @AllArgsConstructor | ||
| public static class ErrorDetails { | ||
| private int status; | ||
| private String message; | ||
| private String details; | ||
| private String endpoint; | ||
| private String method; | ||
| private String timestamp; | ||
| private String requestId; | ||
| } | ||
| } |
8 changes: 4 additions & 4 deletions
8
backend-service/src/main/java/co/teamsphere/api/services/AuthenticationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,17 @@ | ||
| package co.teamsphere.api.services; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import co.teamsphere.api.exception.ProfileImageException; | ||
| import co.teamsphere.api.exception.UserException; | ||
| import co.teamsphere.api.request.SignupRequest; | ||
| import co.teamsphere.api.response.AuthResponse; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @Service | ||
| public interface AuthenticationService { | ||
| AuthResponse signupUser(@Valid SignupRequest request) throws UserException, ProfileImageException; | ||
| AuthResponse signupUser(@Valid SignupRequest request) throws UserException, ProfileImageException, IOException; | ||
|
|
||
| AuthResponse loginUser(String username, String password) throws UserException; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this part, we have a global exception handler, and they get propagated from there as errors. i would move these or add the error handling to there.