簡體   English   中英

承諾:未捕獲的未處理異常

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM