Timestream写入记录遇Cannot read httpResponse of undefined,如何读取错误信息?
解决Timestream写入时无法读取RejectedRecords的问题
你遇到的Cannot read httpResponse of undefined错误,根源是调用.promise()后,request变量变成了Promise实例,不再持有原始请求的response属性。AWS SDK抛出的RejectedRecordsException错误对象本身就包含了拒绝记录的详细信息,不需要手动解析响应体。
修改后的代码如下:
const timestreamParams = { DatabaseName: db_name, TableName: TimeSpanRawE[table_name], Records: chunk, } try { const data = await writeClient.writeRecords(timestreamParams).promise(); return ok(data); } catch (error) { console.log('Error writing records:', error); if (error.code === 'RejectedRecordsException') { // 直接从错误对象中读取拒绝记录 console.log('RejectedRecords: ', error.rejectedRecords); console.log('Other records were written successfully. '); } return err({ error, params: timestreamParams }); }
关键修改点:
- 改用
try/catch替代then/catch,避免Promise实例与原始请求对象混淆 - 直接访问
error.rejectedRecords获取拒绝记录,无需依赖request.response - 代码逻辑更清晰,彻底消除
httpResponse undefined的报错
内容的提问来源于stack exchange,提问作者Ilijanovic
相关产品推荐
相关产品推荐

