package com.whyc.exception.first;
|
|
import com.whyc.dto.Response;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
/**
|
* 1.针对RestController层捕捉异常,结果统一返回
|
* 2.异常日志,本地独立部署测试日志记录 TODO
|
*/
|
@Slf4j
|
@RestControllerAdvice(annotations = RestController.class)
|
public class CustomExceptionResultHandler<T> {
|
|
@ExceptionHandler(DefinedException.class)
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public Response sendErrorResponse2Defined(Exception exception){
|
return new Response().set(0,((DefinedException)exception).getMessage());
|
}
|
|
@ExceptionHandler(Exception.class)
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public Response sendErrorResponse2System(Exception exception){
|
exception.printStackTrace();
|
if(exception instanceof DefinedException) {
|
return this.sendErrorResponse2Defined(exception);
|
}
|
CodeAndMsg.FAIL.setMessage(exception.toString());
|
log.error("发生异常:{}",exception.toString());
|
return new Response().setMsg(0,exception.toString());
|
}
|
|
}
|