已关闭
[Bug] HarmonyOS 截图失败时错误信息显示 undefined #34
sunkaiyang创建于 9月1日关闭于 9月5日
9月1日 关联了pull request:fix: restore ViewShot component flow definitions
cpf-thirdparty-manager
9月2日 评论:
9月2日 评论:
本Issue主要用于开发协作,欢迎在此反馈问题或提出建议


9月3日 修改了issue 的描述
9月3日 修改标题为 “[Bug] HarmonyOS 截图失败时错误信息显示 undefined”,原标题为“fix: restore ViewShot component flow definitions”
9月3日 修改标题为 “[Bug] HarmonyOS 截图失败时错误信息显示 undefined”,原标题为“fix: restore ViewShot component flow definitions”
9月3日 关联了pull request:fix: normalize snapshot errors on HarmonyOS
9月3日 修改了issue 的描述
9月3日 关联了pull request:fix: normalize snapshot errors on HarmonyOS
9月3日 修改了issue 的描述
9月5日 关闭了 issue
[Bug] HarmonyOS 截图失败时错误信息显示 undefined
问题描述
在 React Native OpenHarmony 0.77 环境中,使用
react-native-view-shot截取零尺寸或当前不可截图的 View 时,componentSnapshot.get()会执行失败流程。在该场景下,底层 Promise 可能直接 reject 数字错误码
100001,而不是带有message、code属性的标准BusinessError对象。当前 0.77 实现固定读取err.message,因此底层错误内容在转换过程中丢失,最终传递到 JavaScript 层的错误信息为:相同 Demo 在 React Native OpenHarmony 0.82 环境中能够正确保留底层错误内容,返回:
该问题会导致调用方无法根据错误信息判断截图失败的真实原因,也会降低日志的排障价值。
复现步骤
react-native-view-shotExample 工程。ViewShotContinuousTest测试页面。width和height设置为0。captureMode="continuous"再次触发截图。captureFailed日志。预期结果
截图失败后,应及时触发
onCaptureFailure或 Promise rejection,并保留底层返回的实际错误内容。预期错误信息示例:
错误信息中不应出现
undefined。实际结果
截图进入失败流程,但传递到 JavaScript 层的错误信息为:
底层返回的
100001在读取err.message时丢失。复现概率
必现(100%)。
环境信息
0.77.220.77.74@react-native-ohos/react-native-view-shot:3.9.1-beta.1857d6d477fix0.82.30@react-native-ohos/react-native-view-shot:4.0.0-rc.1DEMO(如有)
复现用例:
关键代码:
<ViewShot ref={myText} style={{ backgroundColor: '#bfa', height: isVisible ? 60 : 0, width: isVisible ? '100%' : 0, overflow: 'hidden', }} captureMode="continuous" onCaptureFailure={getError} onCapture={getSuccess} options={{ format: 'png' }} > {isVisible && <Text>测试截图</Text>} </ViewShot>隐藏 ViewShot:
const toggleVisibility = () => { setIsVisible(!isVisible); };失败回调:
const getError = (err) => { console.log('captureFailed', err.message); setErrMsg(err.message); };日志/截图对比
text<br>captureFailed componentSnapshot failed: undefined<br>text<br>captureFailed componentSnapshot failed: 100001<br>影响范围
受影响的功能和场景包括:
captureRef调用componentSnapshot.get()失败的场景。BusinessError数据的场景。onCaptureFailure或 Promise.catch()获取失败原因的业务。captureScreen及图片转换、文件保存等需要向 JavaScript 层传递异步错误的路径。正常截图成功路径、公开接口签名、参数类型和返回类型不受影响。
根因分析
0.77 实现将 Promise rejection 参数声明为
BusinessError,并直接读取:err.code err.message类型声明不会改变运行时收到的数据。当
componentSnapshot.get()实际 reject 数字100001时,该值没有message属性,因此下面的错误转换产生了undefined:const error = new Error(`componentSnapshot failed: ${err.message}`);修复方案
参考 0.82 的错误处理方式,增加统一的错误类型和错误信息转换方法:
ViewShotError统一保存错误信息和错误码。getErrorMessage()处理标准Error和非标准 rejection 数据。getErrorCode()统一获取错误码或回退到E_UNABLE_TO_SNAPSHOT。captureRef和captureScreen异步失败路径的错误转换。对应修复提交:
兼容性说明
undefined修正为底层实际错误内容。