0
我正在使用嵌套的Promise在before()掛鉤中設置測試數據庫。 它運行良好,但我不知道如何捕捉最高級別的錯誤(在before()鉤子中)。 展望中的console.log,在AA嵌套的承諾被逮住一個錯誤,它返回到以前的水平,值......不匹配..node.js承諾如何處理嵌套Promise中最高調用級別的錯誤
group.test.js
before(() => {
return utils.clearCollections()
.then(() => {
return utils.createSuperAdmin()
.then(() => {
return utils.createTestGroups(3)
.then((insertedGroups) => {
groups = insertedGroups;
return utils.addRolesInGroups(groups)
.then((insertedRoles) => {
console.log('DONE ! %j', insertedRoles);
})
.catch((e) => {
console.log('error %j', e);
});
});
});
});
});
test.utility.js
export function addRolesInGroups(groups) {
...
return Role.insertMany([roleA1, roleA2, roleA3, roleB2, roleB3, roleC3, role4 ])
.then((insertedRoles) => {
console.log('Successfully created test roles in groups');
return insertedRoles;
})
.catch((e) => {
console.log('Error inserting many: %j', e);
return e;
});
}
的console.log
的誤差在無極功能addRolesInGroups() 但它返回到之前(主叫)掛鉤..沒有釣到corrrectly逮住......
Error inserting many: {"code":11000,"index":3,"errmsg":"E11000
duplicate key error collection: cockpit-api-test.roles index: name_1
dup key: { : \"manager\" }","op": "__v":0,
"_groupId":"5954d45e05206235d308f4d6",
"name":"manager","description":"can R group, can RW
user","_id":"5954d45e05206235d308f4db","rolePermissions"
[],"permissions":[{"resourceName":"group","authorizedActions"
["read"]},{"resourceName":"user","authorizedActions"
["read","write"]}],"users":[]}}
DONE ! {"code":11000,"index":3,"errmsg":"E11000 duplicate key error
collection: cockpit-api-test.roles index: name_1
dup key: { : \"manager\" }","op"
{"__v":0,"_groupId":"5954d45e05206235d308f4d6",
"name":"manager","description":"can R group, can RW
user","_id":"5954d45e05206235d308f4db","rolePermissions":[],
"permissions":[{"resourceName":"group","authorizedActions":["read"]}
{"resourceName":"user","authorizedActions":["read","write"]}]
,"users":[]}}
你會想在'catch'處理程序重新拋出錯誤'扔e',否則不能再次捕獲。 – Bergi