-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨feat: add room not exist socket exception
- Loading branch information
1 parent
447b7eb
commit 90cd17d
Showing
8 changed files
with
68 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
27 changes: 19 additions & 8 deletions
27
src/main/java/com/videocall/server/exception/GlobalSocketExceptionHandler.java
This file contains 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,23 +1,34 @@ | ||
package com.videocall.server.exception; | ||
|
||
import com.videocall.server.dto.ApiResponse; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.messaging.handler.annotation.MessageExceptionHandler; | ||
import org.springframework.messaging.simp.annotation.SendToUser; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
|
||
import com.videocall.server.dto.ApiResponse; | ||
|
||
@ControllerAdvice | ||
@RequiredArgsConstructor | ||
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
public class GlobalSocketExceptionHandler { | ||
SimpMessagingTemplate simpMessagingTemplate; | ||
|
||
|
||
@MessageExceptionHandler(AppException.class) | ||
@SendToUser("/topic/errors") | ||
public ResponseEntity<ApiResponse> handleAppException(AppException e) { | ||
@MessageExceptionHandler(SocketException.class) | ||
public ResponseEntity<ApiResponse> handleAppException(SocketException e) { | ||
ErrorCode errorCode = e.getErrorCode(); | ||
|
||
return ResponseEntity.status(errorCode.getCode()).body(ApiResponse.builder() | ||
// Tạo thông báo lỗi | ||
ApiResponse errorResponse = ApiResponse.builder() | ||
.message(e.getMessage()) | ||
.code(errorCode.getCode()) | ||
.build()); | ||
.build(); | ||
|
||
// Gửi thông báo lỗi tới /user/{userId}/topic/errors | ||
simpMessagingTemplate.convertAndSendToUser(e.getUserId(), "/topic/errors", errorResponse); | ||
|
||
return ResponseEntity.badRequest().body(errorResponse); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/videocall/server/exception/SocketException.java
This file contains 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,18 @@ | ||
package com.videocall.server.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class SocketException extends RuntimeException { | ||
|
||
public SocketException(ErrorCode errorCode, String userId) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
this.userId = userId; | ||
} | ||
|
||
private ErrorCode errorCode; | ||
private String userId; | ||
} |
This file contains 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