refactor(exception): 优化异常处理逻辑
- 新增 ExceptionUtil工具类,用于统一处理异常 - 在 V1DatasetController 中使用 ExceptionUtil.difyException 方法包裹可能抛出异常的代码 - 移除 V1ChatController 中未使用的 Mono 导包 - 删除 ApiExceptionHandler 中的 TODO 注释
This commit is contained in:
parent
4549317bb2
commit
9626d39c19
@ -64,5 +64,5 @@ public class ApiExceptionHandler {
|
||||
log.error("ApiException: {}", objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())));
|
||||
return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
//TODo 403报错修改
|
||||
|
||||
}
|
||||
|
18
src/main/java/com/zsc/edu/dify/exception/ExceptionUtil.java
Normal file
18
src/main/java/com/zsc/edu/dify/exception/ExceptionUtil.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.zsc.edu.dify.exception;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Component
|
||||
public class ExceptionUtil {
|
||||
|
||||
public static <T> T difyException(Supplier<T> supplier) throws ApiException {
|
||||
try {
|
||||
return supplier.get();
|
||||
} catch (RuntimeException e) {
|
||||
System.err.println(e.getMessage());
|
||||
throw new ApiException("服务器错误");
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author yanghq
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.zsc.edu.dify.modules.dify.controller;
|
||||
|
||||
import com.zsc.edu.dify.exception.ApiException;
|
||||
import com.zsc.edu.dify.exception.ConstraintException;
|
||||
import com.zsc.edu.dify.exception.ExceptionUtil;
|
||||
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
||||
import io.github.guoshiqiufeng.dify.core.pojo.DifyPageResult;
|
||||
import io.github.guoshiqiufeng.dify.dataset.DifyDataset;
|
||||
@ -43,7 +46,7 @@ public class V1DatasetController {
|
||||
@PreAuthorize("hasAuthority('dify:dataset:create')")
|
||||
@OperationLogAnnotation(content = "'dify知识库文档'", operationType = "新建")
|
||||
public DocumentCreateResponse createDocumentByFile(DocumentCreateByFileRequest request){
|
||||
return difyDataset.createDocumentByFile(request);
|
||||
return ExceptionUtil.difyException(() -> difyDataset.createDocumentByFile(request));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user