You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.4 KiB
51 lines
1.4 KiB
package net.educoder.handler;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import net.educoder.exception.BusinessException;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
/**
|
|
* @Author: youys
|
|
* @Date: 2022/8/16
|
|
* @Description: 全局异常处理
|
|
*/
|
|
|
|
@Slf4j
|
|
@ControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
@ResponseBody
|
|
public ResponseEntity<JSONObject> handleBridgeExceptions(BusinessException e) {
|
|
log.error("捕获到BusinessException", e);
|
|
|
|
JSONObject jsonObj = new JSONObject();
|
|
jsonObj.put("code", e.getCode());
|
|
jsonObj.put("msg", e.getMsg());
|
|
|
|
return new ResponseEntity<>(jsonObj, HttpStatus.OK);
|
|
}
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
@ResponseBody
|
|
public ResponseEntity<JSONObject> handleOtherExceptions(Exception e) {
|
|
log.error("捕获到Exception", e);
|
|
|
|
JSONObject jsonObj = new JSONObject();
|
|
|
|
if (e.getMessage().contains("Maximum upload size exceeded")) {
|
|
jsonObj.put("code", -1);
|
|
jsonObj.put("msg", "未知错误!" + e.getMessage());
|
|
}
|
|
|
|
return new ResponseEntity<>(jsonObj, HttpStatus.OK);
|
|
}
|
|
|
|
|
|
}
|