zhoushihao
2024-04-18 58dc9fa8de7b66ef375aa191da873813cbfae009
hangzhoumesParent/common/servicebase/src/main/java/com/mes/common/exception/GlobalExceptionHandler.java
New file
@@ -0,0 +1,32 @@
package com.mes.common.exception;
import com.mes.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @author zhoush
 * @Date 2024/1/26 15:31
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 如果是serviceExcaption,则调用该方法
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result handle(ServiceException se) {
        return Result.error(se.getCode(), se.getMessage());
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> error(Exception e) {
        e.printStackTrace();
        return Result.error();
    }
}