[英]es6 Promises: exception thrown in a promise is not caught by the catch method
[英]promises: unhandled exception not caught
我有一种身份验证方法,您可以在下面找到。 在该方法中,我根据授予类型调用了不同的Joi Schema验证和令牌验证功能。 该代码基本上可以很好地运行,但是如果我在方法“ checkSchema”中抛出错误,我会在日志中获得完整的堆栈跟踪信息,尽管应该用hapis方法重播来处理该错误。
如果很重要,我会使用蓝鸟承诺。
这里编码:
/**
* checks if the given login informations are right and returns a token response
* For each strategy you first validate the given payload and next you check if the given payload is ok.
*/
export function authenticate(request, reply) {
var schema, checkMethod;
switch (request.payload.grantType) {
case "password":
schema = passwordSchema;
checkMethod = checkPasswordLogin;
break;
case "refreshToken":
schema = refreshTokenSchema;
checkMethod = checkRefreshTokenLogin;
break;
default:
throw new Error("No valid Grant Type given");
}
var promise = Promise
.try(function() {
return checkSchema(request.payload, schema);
})
.then(function(value) {
return checkMethod(value);
})
.then(function(userInstance) {
return generateToken(userInstance);
});
reply(promise);
}
/**
* checks if a payload followed a specific schema and throws an error if not
*/
function checkSchema(payload, schema) {
try {
Joi.assert(payload, schema);
} catch (e) {
throw Boom.create(400, e);
}
}
如果架构失败,这里是堆栈跟踪:
Unhandled rejection Error: Error: {
"grantType": "password",
"username": "John",
"password" [1]: -- missing --
}
[1] "password" is required
at Object.exports.create (/server/test-backend/node_modules/boom/lib/index.
js:21:17)
at checkSchema (/source/api/controllers/app/auth.ts:48:20)
at /source/api/controllers/app/auth.ts:29:20
at tryCatcher (/server/test-backend/node_modules/bluebird/js/main/util.js:2
6:23)
at Function.Promise.attempt.Promise.try (/server/test-backend/node_modules/
bluebird/js/main/method.js:31:24)
at authenticate (/source/api/controllers/app/auth.ts:28:13)
at Object.internals.handler (/server/test-backend/node_modules/hapi/lib/han
dler.js:94:36)
at /server/test-backend/node_modules/hapi/lib/handler.js:28:23
at [object Object].internals.Protect.run (/server/test-backend/node_modules
/hapi/lib/protect.js:56:5)
at exports.execute (/server/test-backend/node_modules/hapi/lib/handler.js:2
2:22)
at /server/test-backend/node_modules/hapi/lib/request.js:370:13
at iterate (/server/test-backend/node_modules/hapi/node_modules/items/lib/i
ndex.js:35:13)
at done (/server/test-backend/node_modules/hapi/node_modules/items/lib/inde
x.js:27:25)
at /server/test-backend/node_modules/hoek/lib/index.js:841:22
at /server/test-backend/node_modules/continuation-local-storage/node_module
s/async-listener/glue.js:188:31
at process._tickDomainCallback [as _tickCallback] (node.js:486:13)
我目前通过编写下面的代码来修复它。 不幸的是,我不知道为什么如果我在checkMethod
抛出一个错误,一切都很好,但是如果我在checkSchema
抛出一个错误,我需要手工捕获所有错误(hapi js不会为我处理)。
/**
* checks if the given login informations are right and returns a token response
* For each strategy you first validate the given payload and next you check if the given payload is ok.
*/
export function authenticate(request, reply) {
var schema, checkMethod;
switch (request.payload.grantType) {
case "password":
schema = passwordSchema;
checkMethod = checkPasswordLogin;
break;
case "refreshToken":
schema = refreshTokenSchema;
checkMethod = checkRefreshTokenLogin;
break;
default:
throw new Error("No valid Grant Type given");
}
var promise = Promise
.try(function() {
return checkSchema(request.payload, schema);
})
.then(function() {
return checkMethod(request.payload);
})
.then(function(userInstance) {
return generateToken(userInstance);
})
.catch(function(err) {
return err;
});
reply(promise);
}
/**
* checks if a payload followed a specific schema and throws an error if not
*/
function checkSchema(payload, schema) {
var result = Joi.validate(payload, schema);
if (result.error) {
return Promise.reject(Boom.wrap(result.error, 400));
}
return Promise.resolve(result.value);
}
我没有完全解决您的问题,但是据我了解,您在处理诺言时不应抛出错误。 如果有错误,请reject
诺言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.