ZengTao
2024-04-18 f8a73ae78727eab0063a26b4c57c3d451f1a0ab5
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();
    }
}