我正在尝试将一些数据插入 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 打字稿中的所有异常类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)