Commit 8db1819b by 林永健

feat(store): 添加策略详情查询接口并优化错误处理

- 添加了 queryStrategyInfoById 接口用于查询策略详情
- 更新了 selectStrategyInfoPage 错误处理逻辑,移除冗余错误信息
- 新增 RpcServiceException 导入用于异常处理
- 添加了完整的 API 文档注释和异常捕获机制
- 统一了错误响应格式,提高接口稳定性
1 parent 87316690
......@@ -12,6 +12,7 @@ import cn.com.glsx.dj.strategy.remote.StrategyInfoRemote;
import cn.com.glsx.dj.util.CommonUtil;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.rop.response.RopResponse;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
......@@ -88,7 +89,7 @@ public class StoreCommentController {
RpcResponse<RpcPagination<StrategyInfo>> response = strategyInfoRemote.selectStrategyInfoPage(pagination);
if (response.hasError()) {
log.error(response.getMessage());
return RopResponse.error(response.getError(), response.getMessage());
return RopResponse.error(response.getMessage());
}
LoginUserInfoResponse loginUserInfo = CommonUtil.getLoginUserInfo();
return CommonUtil.convertPaginationRopResponse(response, source -> {
......@@ -102,4 +103,23 @@ public class StoreCommentController {
return RopResponse.error(e.getMessage());
}
}
@ApiOperation("查询策略详情(学习、课程、带教申请、协同类策略:人员更换评估/商务政策更新评估/多品类拓张/流量问题改善)")
@ServiceMethod(method = "dj.api.pe.analysis.store.strategy.queryStrategyInfoById", needInSession = NeedInSessionType.YES)
public RopResponse<StrategyInfo> queryStrategyInfoById(CommonIdRequest request) {
try {
RpcResponse<StrategyInfo> response = strategyInfoRemote.queryStrategyInfoById(request.getId());
if (response.hasError()) {
log.error(response.getMessage());
return RopResponse.error(response.getMessage());
}
return RopResponse.success(response.getResult());
} catch (RpcServiceException e) {
log.error("=====查询策略详情 rpcServiceException:{}", e.getMessage());
return RopResponse.error(e);
} catch (Exception e) {
log.error("=====查询策略详情 exception:{}", e.getMessage(), e);
return RopResponse.error(e.getMessage());
}
}
}
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 缺少入参校验(功能实现的正确性与健壮性)

    • 问题描述:在新增的 queryStrategyInfoById 方法中,直接调用了 request.getId(),未对 request 对象以及 request.getId() 进行非空判断。
    • 影响分析:如果调用方未传递参数或 request 为 null,执行 request.getId() 会直接引发 NullPointerException;如果 id 为空,则会向 RPC 服务发起无效请求,造成不必要的网络开销,甚至引发下游服务的异常。
    • 优化建议:在方法开头增加参数的非空校验(手动校验或使用 Validator 注解)。 java public RopResponse<StrategyInfo> queryStrategyInfoById(CommonIdRequest request) { if (request == null || request.getId() == null) { return RopResponse.error("请求参数或ID不能为空"); } try { // ...

    2. 异常信息直接透传前端及潜在NPE(安全性与健壮性)

    • 问题描述:在捕获通用异常 catch (Exception e) 时,代码直接使用 RopResponse.error(e.getMessage()) 将异常信息返回给调用方。
    • 影响分析:
      1. 安全性风险:底层异常可能包含数据库 SQL、文件路径、框架类名等敏感的系统内部实现细节,直接暴露给前端存在安全隐患。
      2. 健壮性问题:如果系统抛出的是 NullPointerException,此时 e.getMessage() 的值为 null,会导致返回给前端的错误提示为空,前端无法正确解析或给用户良好的提示。
    • 优化建议:对于兜底的通用异常,建议返回统一的、模糊的错误提示,避免暴露内部细节。 java } catch (Exception e) { // 记录带有上下文的详细日志 log.error("=====查询策略详情 exception, id:{}, message:{}", request.getId(), e.getMessage(), e); // 返回对用户友好的统一提示 return RopResponse.error("系统内部异常,请稍后重试"); }

    3. 错误日志缺少上下文信息(最佳实践与可维护性)

    • 问题描述:在处理 RPC 错误和异常时,日志打印(如 log.error(response.getMessage()); 和 log.error("=====查询策略详情 rpcServiceException:{}", e.getMessage());)仅记录了错误信息,未记录引发该错误的请求参数(如 id)。
    • 影响分析:在生产环境中,面对海量日志,仅凭一句错误描述很难快速定位是哪一条业务数据触发了问题,大幅增加了排查和定位 Bug 的成本。
    • 优化建议:在打印错误日志时,将关键的业务标识(如 request.getId())一同打印,提供完整的上下文信息。 java if (response.hasError()) { log.error("查询策略详情失败, id:{}, error:{}", request.getId(), response.getMessage()); return RopResponse.error(response.getMessage()); }

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):核心逻辑调用正确,异常捕获机制完整。但缺失了针对外部请求参数的基本防御性校验,扣除 5 分。
    • 安全性与潜在风险(25/30分):兜底异常处理将 Exception.getMessage() 直接作为响应内容返回,存在潜在的内部敏感信息泄露风险,扣除 5 分。
    • 最佳实践与可维护性(17/20分):代码结构清晰,且使用了 Swagger 注解增强了 API 的可读性。但错误日志缺失关键的上下文追踪信息,扣除 3 分。
    • 性能与资源利用(5/5分):逻辑简单直接,未见明显的性能瓶颈或内存泄漏隐患,资源利用合理。
    • 提交信息质量(5/5分):Commit message 格式规范,清晰列出了新增特性、优化点以及变更目的,具有很好的可追溯性。满分。

    总分:87分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!