AtmsExceptionHandler.java 1.53 KB
Newer Older
neo's avatar
neo committed
1 2 3 4 5 6 7
package pwc.taxtech.atms.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
8
import pwc.taxtech.atms.exception.ApiException;
neo's avatar
neo committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.exception.ServiceException;

@ControllerAdvice
public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = {
            ApplicationException.class,
            ServiceException.class
    })
    protected ResponseEntity<Object> handleExceptions(Exception ex) throws ServiceException {
        logger.error("Rest Exception!", ex);
        if (ex instanceof ApplicationException) {
            return handleApplicationException((ApplicationException) ex);
        } else if (ex instanceof ServiceException) {
            return handleServiceException((ServiceException) ex);
25 26
        } else if (ex instanceof ApiException) {
            return ((ApiException) ex).handle();
neo's avatar
neo committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    private ResponseEntity<Object> handleApplicationException(ApplicationException ex) {
        throw ex;
    }

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

}