You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS SDK清理SecurityHub时遇资源不存在异常及分支判断异常排查

AWS SecurityHub 清理异常排查及根因分析

问题概述

  • 使用AWS SDK清理亚太区域(新加坡,ap-southeast-1)的SecurityHub时,触发异常:The request is rejected since no such resource found.
  • 已编写多区域清理的try-catch逻辑,但在该特定区域中,即便异常消息完全匹配上述内容,代码仍会进入else分支,需明确异常产生的根因及逻辑问题。

涉及代码片段

try
{
 var securityHubIssue = 'The request is rejected since no such resource found.'
    //delete code
 
}
catch (Exception ex)
{
    if (ex.Message.Contains(securityHubIssue))
    {
        var logDescription = string.Format("Cleanup excluded in aws account {0} due to the reason with exception: {1},in region {2}", subscription.Guid, ex.Message, region);
        
    }
    else
    {
        var logDescription = string.Format("Cleanup failed in AWS account {0}, Failed to delete Invitations in SecurityHub with exception: {1},in region {2}", subscription.Guid, ex.Message, region);
    }
}

根因分析

1. 代码语法与字符串匹配问题

  • C#中字符串定义需使用双引号",代码里用单引号'定义securityHubIssue属于语法错误,会导致编译失败或变量未正确初始化,后续Contains判断永远不成立,直接进入else分支。
  • 即便忽略语法问题,AWS SDK返回的异常消息可能存在大小写差异、额外空格、本地化格式调整(比如新加坡区域的异常消息可能附带区域相关标识),导致Contains匹配失效。

2. SecurityHub资源状态问题

  • 目标区域ap-southeast-1内,SecurityHub可能从未启用,或待清理的资源(如邀请、配置项)已被提前删除,执行删除操作时自然找不到对应资源,抛出该异常。
  • 多区域清理逻辑未提前校验区域内SecurityHub的启用状态,直接执行删除操作,导致无意义的异常触发。

3. AWS SDK异常结构问题

  • AWS SDK抛出的异常多为AmazonServiceException子类,核心错误信息通常存储在ex.Error.Message中,而非基础Exception.Message。直接判断ex.Message可能获取到的是封装后的通用提示,而非目标错误文本,导致匹配失败。

内容的提问来源于stack exchange,提问作者Sakkulsum Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:12:46