package cn.autoform.fw.exception; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import cn.autoform.fw.utility.RestResultGenerator; /** * @author yangrx * RestExceptionHandler * */ @RestControllerAdvice public class RestExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class); @ExceptionHandler @ResponseBody // @ResponseStatus(HttpStatus.BAD_REQUEST) private RestResult runtimeExceptionHandler(Exception e) { // BAD LOGIN if (e instanceof RowOverLengthException) { // ROWOVERLENGTH LOGGER.error("---------> ROWOVERLENGTH!", e); return RestResultGenerator.genErrorResult("100004"); }else if(e instanceof FormClientException){ LOGGER.error("---------> FORM CLIENT REEOR!", e); // return RestResultGenerator.genErrorResult("100011"); RestResult rr = RestResult.newInstance(); rr.setRspCode(((FormClientException)e).getCode()); rr.setRspDesc(((FormClientException)e).getName()); return rr; }else if(e instanceof IEMRuntimeException) { RestResult rr = RestResult.newInstance(); rr.setRspCode(((IEMRuntimeException)e).getCode()); rr.setRspDesc(((IEMRuntimeException)e).getMessage()); return rr; }else if(e instanceof LoginException){ LOGGER.error("SYSTEM ERROR!", e); return RestResultGenerator.genErrorResult("L0001"); }else{ LOGGER.error("SYSTEM ERROR!", e); return RestResultGenerator.genErrorResult("100001"); } } @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) private RestResult illegalParamsExceptionHandler(MethodArgumentNotValidException e) { LOGGER.error("---------> invalid request!", e); return RestResultGenerator.genErrorResult("100002"); } }