|
|
|
package com.teaching.backend.exception;
|
|
|
|
|
|
|
|
import com.teaching.backend.common.BaseResponse;
|
|
|
|
import com.teaching.backend.common.ErrorCode;
|
|
|
|
import com.teaching.backend.common.ResultUtils;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
|
|
|
|
@RestControllerAdvice
|
|
|
|
public class GlobalExceptionHandler {
|
|
|
|
|
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
|
|
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException ex) {
|
|
|
|
ErrorResponse errorResponse = new ErrorResponse(ex.getCode(), ex.getMessage());
|
|
|
|
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ExceptionHandler(Exception.class)
|
|
|
|
// public BaseResponse handleException(Exception ex) {
|
|
|
|
// return ResultUtils.error(ErrorCode.UNKNOW_ERROR, ex.getMessage());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 其他异常处理器可以按需添加
|
|
|
|
|
|
|
|
public static class ErrorResponse {
|
|
|
|
private int code;
|
|
|
|
private String message;
|
|
|
|
|
|
|
|
public ErrorResponse(int code, String message) {
|
|
|
|
this.code = code;
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCode() {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCode(int code) {
|
|
|
|
this.code = code;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMessage() {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMessage(String message) {
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|