Hyperledger Composer REST Server即使事务失败也返回200

时间:2018-07-26 20:05:44

标签: rest hyperledger hyperledger-composer

我正在尝试使Hyperledger Composer REST服务器在事务失败时返回500错误代码,但只会返回200。

这是交易:

/**
 * Move a player totem
 * @param {org.pandemic.board.MoveTotem} txData
 * @transaction
 */
function moveTotem(txData) {
    let moveType = txData.moveType;
    let boardId = txData.boardId;
    let totemName = txData.totemName;
    let destination = txData.destination;

    switch(moveType) {
        case "DRIVE_FERRY":
            driveFerry(boardId, totemName, destination);
            break;
        case "DIRECT_FLIGHT":
            directFlight(boardId, totemName, destination);
            break;
        case "CHARTER_FLIGHT":
            charterFlight(boardId, totemName, destination);
            break;
        case "SHUTTLE_FLIGHT":
            shuttleFlight(boardId, totemName, destination);
            break;
        default:
            throw new Error("Invalid move type specified");
    }
}

特别是第一个case语句:

function driveFerry(boardId, totemName, destination) {
    return getAssetRegistry('org.pandemic.board.Board').then((registry) => {

        return registry.get(boardId).then((board) => {
            let playerIdx = getPlayerTotemIdx(board, totemName);

            checkRemainingActions(board, playerIdx);

            let currentBoardCity = getCurrentBoardCityForPlayer(board, playerIdx);

            if(currentBoardCity == null || currentBoardCity.length == 0) {
                throw new Error("Player is not in a city, how did that happen? Ending game...");
                //TODO: end the game
            }

            if(currentBoardCity.connections.indexOf(destination) > -1) {
                board.players[playerIdx].currentLocation = destination;
                board.players[playerIdx].actionsRemaining -= 1;
                return registry.update(board);
            } else {
                return Promise.reject("Destination is not connected to current city");
               //throw new Error();
            }

        });
    });
}

我已经尝试过throw new Error()和(如上所示)return Promise.reject(),但是返回到前端的状态代码始终为200,加上我返回transactionId / timestamp,并且事务在历史记录。我知道交易没有提交,因为我事后检查了世界状态,并且这些值是我希望看到的(未更改)。

1 个答案:

答案 0 :(得分:1)

我知道了!我需要return从事务代码中初始switch语句的函数调用,如下所示:

/**
 * Move a player totem
 * @param {org.pandemic.board.MoveTotem} txData
 * @transaction
 */
function moveTotem(txData) {
    let moveType = txData.moveType;
    let boardId = txData.boardId;
    let totemName = txData.totemName;
    let destination = txData.destination;

    switch(moveType) {
        case "DRIVE_FERRY":
            return driveFerry(boardId, totemName, destination);
        case "DIRECT_FLIGHT":
            return directFlight(boardId, totemName, destination);
        case "CHARTER_FLIGHT":
            return charterFlight(boardId, totemName, destination);
        case "SHUTTLE_FLIGHT":
            return shuttleFlight(boardId, totemName, destination);
    }
}

这样,承诺链就不会中断,然后在我希望引发错误的地方,我需要使用return Promise.reject(...)

相关问题