xyc
5 天以前 972ec748b0b1cd301c04407045e713e560b8d2db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.trafficaudit.common;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
 
/** 全局异常处理:把 500 错误原因透出到前端提示(P0:导出失败可见具体原因) */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
 
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Result<String>> handle(Exception e) {
        log.error("Unhandled exception", e);
        String msg = e.getMessage();
        if (msg == null || msg.trim().isEmpty()) msg = e.toString();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(Result.error(500, msg));
    }
}