问题描述
我正在尝试将一些数据插入 dynamodb,并且正如预期的那样,我得到了一个 ConditionalCheckFailedException
.因此,我试图仅针对该场景捕获该异常,除此之外,我想为所有其他错误引发服务器错误.但是要添加类型,我无法在 aws-sdk 中找到 ConditionalCheckFailedException
.
I am trying to insert some data into dynamodb, and as expected I am getting a ConditionalCheckFailedException
. So I am trying to catch that exception for only that scenario, apart from that I want to throw server error for all other errors.
But to add the type, I am not able to find the ConditionalCheckFailedException
in aws-sdk.
这就是我想要做的.
// where to import this from
try {
await AWS.putItem(params).promise()
} catch (e) {
if (e instanceof ConditionalCheckFailedException) { // unable to find this exception type in AWS SDK
throw new Error('create error')
} else {
throw new Error('server error')
}
}
推荐答案
你可以使用下面的守卫来检查错误:
You can check the error by using the following guard instead:
if (e.code === 'ConditionalCheckFailedException') {
请注意,instanceof
仅适用于类,不适用于接口.所以即使你有类型,如果它是一个接口,你也不能使用它,因为它依赖于某些原型检查.使用 err.code
属性更安全.
Note that instanceof
only works on classes, not on interfaces. So even if you had the type, if it was an interface you couldn't use it because it relies on certain prototype checks. Using the err.code
property is safer.
这篇关于在哪里可以找到 AWS-SDK for dynamodb 在 NodeJS 打字稿中的所有异常类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!