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.
53 lines
1.5 KiB
53 lines
1.5 KiB
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; |
|
} |
|
} |
|
} |
|
|
|
|