Skip to content

Commit db89cba

Browse files
authored
Merge pull request #47 from welsir/master
123
2 parents 5dbfd62 + 33f6be7 commit db89cba

29 files changed

+2553
-874
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.github.timemachinelab.controller;
2+
3+
import io.github.timemachinelab.core.session.application.SseNotificationService;
4+
import io.github.timemachinelab.core.session.application.SseValidationService;
5+
import io.github.timemachinelab.core.session.application.SessionException;
6+
import io.github.timemachinelab.entity.resp.ApiResult;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.RestControllerAdvice;
11+
12+
import javax.annotation.Resource;
13+
import javax.servlet.http.HttpServletRequest;
14+
15+
/**
16+
* 全局异常处理器
17+
* 统一处理应用中的异常
18+
*
19+
* @author welsir
20+
* @date 2025/1/20
21+
*/
22+
@Slf4j
23+
@RestControllerAdvice
24+
public class GlobalExceptionHandler {
25+
26+
@Resource
27+
private SseValidationService sseValidationService;
28+
@Resource
29+
SseNotificationService sseNotificationService;
30+
/**
31+
* 处理SSE验证异常
32+
*
33+
* @param e SSE验证异常
34+
* @param request HTTP请求对象
35+
* @return 错误响应
36+
*/
37+
@ExceptionHandler(SessionException.class)
38+
public ResponseEntity<?> handleSessionException(SessionException e, HttpServletRequest request) {
39+
log.warn("SSE验证失败: {}", e.getMessage());
40+
41+
// 尝试通过SSE发送错误消息
42+
sseValidationService.sendSseErrorIfConnected(request, e.getMessage());
43+
sseNotificationService.sendErrorMessage(null, e.getMessage());
44+
// 根据请求路径返回不同格式的响应
45+
String requestPath = request.getRequestURI();
46+
if (requestPath.contains("/retry")) {
47+
return ResponseEntity.badRequest().body(ApiResult.error(e.getMessage()));
48+
} else {
49+
return ResponseEntity.badRequest().body(e.getMessage());
50+
}
51+
}
52+
53+
54+
55+
/**
56+
* 处理通用异常
57+
*
58+
* @param e 异常
59+
* @param request HTTP请求对象
60+
* @return 错误响应
61+
*/
62+
@ExceptionHandler(Exception.class)
63+
public ResponseEntity<?> handleGeneralException(Exception e, HttpServletRequest request) {
64+
log.error("系统异常: {}", e.getMessage(), e);
65+
66+
// 尝试通过SSE发送错误消息
67+
sseValidationService.sendSseErrorIfConnected(request, "系统异常,请重试");
68+
69+
// 根据请求路径返回不同格式的响应
70+
String requestPath = request.getRequestURI();
71+
if (requestPath.contains("/retry")) {
72+
return ResponseEntity.internalServerError().body(ApiResult.error("系统异常: " + e.getMessage()));
73+
} else {
74+
return ResponseEntity.internalServerError().body("系统异常,请重试");
75+
}
76+
}
77+
}

prompto-lab-app/src/main/java/io/github/timemachinelab/controller/SSEDemoController.java

Lines changed: 0 additions & 170 deletions
This file was deleted.

0 commit comments

Comments
 (0)