Commit ea5a057f authored by neo's avatar neo

[DEV] exception handler multi

parent c50d8ab7
package pwc.taxtech.atms.common.exception;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import pwc.taxtech.atms.common.ServiceException;
import pwc.taxtech.atms.dto.ApiResultDto;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@ControllerAdvice
public class CustomExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
@ExceptionHandler(value = Throwable.class)
public ApiResultDto handle(HttpServletResponse response) {
return ApiResultDto.fail("error.");
}
@ExceptionHandler(value = ServiceException.class)
public void customHandle(ServiceException exception, HttpServletResponse response) {
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write(JSON.toJSONString(ApiResultDto.fail(exception.getMessage())));
} catch (IOException e) {
logger.error("customHandle error.", e);
}
}
}
package pwc.taxtech.atms.controller; package pwc.taxtech.atms.controller;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -8,10 +9,14 @@ import org.springframework.web.bind.annotation.ControllerAdvice; ...@@ -8,10 +9,14 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import pwc.taxtech.atms.dto.ApiResultDto;
import pwc.taxtech.atms.exception.ApiException; import pwc.taxtech.atms.exception.ApiException;
import pwc.taxtech.atms.exception.ApplicationException; import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.exception.ServiceException; import pwc.taxtech.atms.exception.ServiceException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@EnableWebMvc @EnableWebMvc
@ControllerAdvice @ControllerAdvice
public class AtmsExceptionHandler extends ResponseEntityExceptionHandler { public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
...@@ -19,7 +24,6 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -19,7 +24,6 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { @ExceptionHandler(value = {
ApplicationException.class, ApplicationException.class,
ServiceException.class,
ApiException.class ApiException.class
}) })
protected ResponseEntity<Object> handleExceptions(Exception ex) throws ServiceException { protected ResponseEntity<Object> handleExceptions(Exception ex) throws ServiceException {
...@@ -45,6 +49,22 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler { ...@@ -45,6 +49,22 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
} }
} }
@ExceptionHandler(value = Throwable.class)
public ApiResultDto handle(HttpServletResponse response) {
return ApiResultDto.fail("error.");
}
@ExceptionHandler(value = ServiceException.class)
public void customHandle(pwc.taxtech.atms.common.ServiceException exception, HttpServletResponse response) {
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write(JSON.toJSONString(ApiResultDto.fail(exception.getMessage())));
} catch (IOException e) {
logger.error("customHandle error.", e);
}
}
private ResponseEntity<Object> handleApplicationException(ApplicationException ex) { private ResponseEntity<Object> handleApplicationException(ApplicationException ex) {
throw ex; throw ex;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment