GlobalControllerExceptionHandler.java 4.37 KB
Newer Older
PWF-WK01\pengwufeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
package com.beecode.inz.common;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
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.multipart.MultipartException;

import com.beecode.bap.attachment.exception.AttachmentDataUploadException;
import com.beecode.bcp.workflow.exception.WorkflowRuntimeException;
import com.beecode.inz.common.exception.BusinessException;
import com.beecode.inz.common.exception.ForbiddenOperateException;
import com.beecode.inz.common.exception.ParameterException;
import com.beecode.inz.common.exception.ResourceNotFoundException;
import com.beecode.inz.common.exception.UnOperateAuthorityException;

/**
 * 
 * @author tanshuqiang 2018年2月12日
 */
@ControllerAdvice()
@Component("com.beecode.inz.common.GlobalControllerExceptionHandler")
public class GlobalControllerExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

	@Autowired
    private ErrorCodeMessageConfigurer errorCodeMessageConfigurer;
	
	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
	@ExceptionHandler(ParameterException.class)
	@ResponseBody
	public RestServiceError handleParameterException(HttpServletRequest req, ParameterException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(UnOperateAuthorityException.class)
	@ResponseBody
	public RestServiceError handleNonAuthorizationException(HttpServletRequest req, UnOperateAuthorityException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.FORBIDDEN)
	@ExceptionHandler(ForbiddenOperateException.class)
	@ResponseBody
	public RestServiceError handleForbiddenOperationException(HttpServletRequest req, ForbiddenOperateException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	@ExceptionHandler(ResourceNotFoundException.class)
	@ResponseBody
	public RestServiceError handleResourceNotFoundException(HttpServletRequest req, ResourceNotFoundException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(BusinessException.class)
	@ResponseBody
	public RestServiceError handleBusinessException(HttpServletRequest req, BusinessException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(AttachmentDataUploadException.class)
	@ResponseBody
	public RestServiceError handleAttachmentDataUploadException(HttpServletRequest req, AttachmentDataUploadException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(WorkflowRuntimeException.class)
	@ResponseBody
	public RestServiceError handleWorkflowRuntimeException(HttpServletRequest req, WorkflowRuntimeException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(MultipartException.class)
	@ResponseBody
	public RestServiceError handleMultipartException(HttpServletRequest req, MultipartException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public RestServiceError handleException(HttpServletRequest req, Exception ex) {
		logError(req, ex);
		return RestServiceError.build(CommonErrorCodeEnum.INTERNAL_SERVER_ERROR, errorCodeMessageConfigurer);
	}

	private void logError(HttpServletRequest req, Throwable ex) {
		logger.error("Request:{}, raised:", req.getRequestURI(), ex);
		ex.printStackTrace();
	}

}