Hyperledger Composer REST 服务器 returns 200 即使交易失败

Hyperledger Composer REST Server returns 200 even when transaction fails

我试图让 Hyperledger Composer REST 服务器在交易失败时 return 500 错误代码,但它只会 return 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 和交易记录在 Historian 记录中。我知道交易没有提交,因为之后我检查了世界状态并且值是我期望看到的(未更改)。

我想通了!我需要 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);
    }
}

这样 promise 链就不会被破坏,然后我想把错误抛出的地方,我需要使用 return Promise.reject(...)