AtmsExceptionHandler.java 4.07 KB
Newer Older
neo's avatar
neo committed
1 2
package pwc.taxtech.atms.controller;

neo's avatar
neo committed
3
import com.alibaba.fastjson.JSON;
neo's avatar
neo committed
4 5
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
neo's avatar
neo committed
6 7
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
gary's avatar
gary committed
8
import org.springframework.security.access.AccessDeniedException;
neo's avatar
neo committed
9 10
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
11
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
neo's avatar
neo committed
12
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
neo's avatar
neo committed
13
import pwc.taxtech.atms.dto.ApiResultDto;
14
import pwc.taxtech.atms.exception.ApiException;
neo's avatar
neo committed
15 16 17
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.exception.ServiceException;

neo's avatar
neo committed
18 19 20
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

21
@EnableWebMvc
neo's avatar
neo committed
22 23
@ControllerAdvice
public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
neo's avatar
neo committed
24
    private static Logger LOGGER = LoggerFactory.getLogger(AtmsExceptionHandler.class);
neo's avatar
neo committed
25 26 27

    @ExceptionHandler(value = {
            ApplicationException.class,
28
            ApiException.class
neo's avatar
neo committed
29 30
    })
    protected ResponseEntity<Object> handleExceptions(Exception ex) throws ServiceException {
neo's avatar
neo committed
31 32 33 34 35 36 37
        LOGGER.error("Rest Exception!", ex);
        ex.printStackTrace();
        if (ex.getMessage() != null) {
            LOGGER.debug("Rest Exception for {}", ex.getMessage());
            LOGGER.info("Rest Exception for {]", ex.getMessage());
        }

neo's avatar
neo committed
38
        if (ex instanceof ApplicationException) {
neo's avatar
neo committed
39
            ex.printStackTrace();
neo's avatar
neo committed
40 41
            return handleApplicationException((ApplicationException) ex);
        } else if (ex instanceof ServiceException) {
neo's avatar
neo committed
42
            ex.printStackTrace();
neo's avatar
neo committed
43
            return handleServiceException((ServiceException) ex);
44
        } else if (ex instanceof ApiException) {
neo's avatar
neo committed
45
            ex.printStackTrace();
46
            return ((ApiException) ex).handle();
neo's avatar
neo committed
47
        } else {
neo's avatar
neo committed
48
            ex.printStackTrace();
neo's avatar
neo committed
49 50 51 52
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

gary's avatar
gary committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    @ExceptionHandler(value = AccessDeniedException.class)
    public void accessDeniedHandle(AccessDeniedException accessDeniedException, HttpServletResponse response) {
        accessDeniedException.printStackTrace();
        //noinspection Duplicates
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=UTF-8");
            response.setStatus(403);
            response.getWriter().write(JSON.toJSONString(ApiResultDto.fail(accessDeniedException.getMessage())));
        } catch (IOException e) {
            logger.error("accessDenied error.", e);
        }
    }

neo's avatar
neo committed
68
    @ExceptionHandler(value = Throwable.class)
eddie.woo's avatar
eddie.woo committed
69 70
    public void handle(Throwable throwable, HttpServletResponse response) {
        throwable.printStackTrace();
eddie.woo's avatar
eddie.woo committed
71
        logger.error("handle : ", throwable);
eddie.woo's avatar
eddie.woo committed
72 73 74 75 76 77 78 79
        //noinspection Duplicates
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().write(JSON.toJSONString(ApiResultDto.fail(throwable.getMessage())));
        } catch (IOException e) {
            logger.error("customHandle error.", e);
        }
neo's avatar
neo committed
80 81 82 83
    }

    @ExceptionHandler(value = ServiceException.class)
    public void customHandle(pwc.taxtech.atms.common.ServiceException exception, HttpServletResponse response) {
eddie.woo's avatar
eddie.woo committed
84
        //noinspection Duplicates
eddie.woo's avatar
eddie.woo committed
85
        logger.error(exception);
neo's avatar
neo committed
86 87 88 89 90 91 92 93 94
        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);
        }
    }

neo's avatar
neo committed
95 96 97 98 99 100 101 102 103
    private ResponseEntity<Object> handleApplicationException(ApplicationException ex) {
        throw ex;
    }

    private ResponseEntity<Object> handleServiceException(ServiceException ex) throws ServiceException {
        throw ex;
    }

}