GlobalExceptionHandler.java 769 Bytes
package com.objecteye.handle;

import com.objecteye.common.CommonResult;
import com.objecteye.exception.CustomXException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public CommonResult globalExceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
        if (e instanceof CustomXException) {
            return CommonResult.success(Integer.parseInt(((CustomXException) e).getCode()), ((CustomXException) e).getMsg(), null);
        }
        return CommonResult.success(504, e.getMessage(), "");
    }
}