daniel0mullins
daniel0mullins

Reputation: 1959

Hyperledger Composer REST Server returns 200 even when transaction fails

I'm trying to get Hyperledger Composer REST server to return a 500 error code when a transaction fails, but it will only return a 200.

Here is the transaction:

/**
 * 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");
    }
}

And specifically the first case statement:

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();
            }

        });
    });
}

I've tried throw new Error() and (as seen above) return Promise.reject() but the status code back to the front-end is always 200, plus I get back the transactionId/timestamp and the transaction is in the Historian records. I know that the transaction isn't committing, because I check the world state afterwards and the values are what I expect to see (unchanged).

Upvotes: 0

Views: 57

Answers (1)

daniel0mullins
daniel0mullins

Reputation: 1959

I figured it out! I need to return the function calls from the initial switch statement in the transaction code, like this:

/**
 * 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);
    }
}

That way the promise chains are unbroken, and then where I want the error to be thrown, I need to use return Promise.reject(...)

Upvotes: 1

Related Questions