-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketGlobalExceptionHandler.java
More file actions
72 lines (61 loc) · 3.18 KB
/
SocketGlobalExceptionHandler.java
File metadata and controls
72 lines (61 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package socket_server.common.exception;
import gotcha_common.exception.ExceptionRes;
import gotcha_common.exception.exceptionCode.GlobalExceptionCode;
import gotcha_domain.auth.SecurityUserDetails;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.ControllerAdvice;
import java.util.HashMap;
import static gotcha_common.exception.exceptionCode.GlobalExceptionCode.USER_NOT_FOUND;
import static socket_server.common.constants.WebSocketConstants.*;
@Slf4j
@ControllerAdvice
@RequiredArgsConstructor
public class SocketGlobalExceptionHandler {
private final SimpMessagingTemplate messagingTemplate;
@MessageExceptionHandler(SocketFieldValidationException.class)
public void handleSocketFieldValidationException(
SocketFieldValidationException e,
SimpMessageHeaderAccessor accessor
) {
log.warn("[WebSocket FieldValidationException] {} - {}", e.getSource(), e.getFieldErrors());
sendErrorToUser(e.getSource(), accessor, ExceptionRes.from(GlobalExceptionCode.FIELD_VALIDATION_ERROR, new HashMap<>(e.getFieldErrors())));
}
@MessageExceptionHandler(SocketCustomException.class)
public void handleSocketCustomException(
SocketCustomException e,
SimpMessageHeaderAccessor accessor
) {
log.warn("[WebSocket CustomException] {} - {}", e.getErrorType(), e.getExceptionCode());
sendErrorToUser(e.getErrorType(), accessor, ExceptionRes.from(e.getExceptionCode()));
}
@MessageExceptionHandler(Exception.class)
@SendToUser("/queue/errors")
public ExceptionRes handleUnexpectedException(Exception e) {
log.error("[WebSocket Unexpected Exception] {} - {}", e.getClass().getSimpleName(), e.getMessage(), e);
return ExceptionRes.from(GlobalExceptionCode.INTERNAL_SERVER_ERROR);
}
private void sendErrorToUser(ErrorType errorType, SimpMessageHeaderAccessor accessor, ExceptionRes response) {
String uuid = findNowUuid(accessor, errorType);
String destination = switch (errorType) {
case ROOM -> ERROR_CHANNEL_PREFIX + ERROR_ROOM_CHANNEL + uuid;
case CHAT -> ERROR_CHANNEL_PREFIX + ERROR_CHAT_CHANNEL + uuid;
case GAME -> ERROR_CHANNEL_PREFIX + ERROR_GAME_CHANNEL + uuid;
case LOBBY -> ERROR_CHANNEL_PREFIX + ERROR_LOBBY_CHANNEL + uuid;
default -> ERROR_CHANNEL_PREFIX + ERROR_DEFAULT_CHANEL + uuid ;
};
messagingTemplate.convertAndSend(destination, response);
}
private static String findNowUuid(SimpMessageHeaderAccessor accessor, ErrorType errorType) {
Authentication auth = (Authentication) accessor.getUser();
if (auth == null) {
throw new SocketCustomException(errorType, USER_NOT_FOUND);
}
return ((SecurityUserDetails) auth.getPrincipal()).getUuid();
}
}