Hyperledger Composer: REST Server transaction failed but returns 200 status

0 votes

I have written a transaction to make Hyperledger Composer REST server to return a 500 error code when a transaction fails, but even when transaction fails, returns 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();
            }

        });
    });
}

Nov 6, 2018 in Blockchain by digger
• 27,620 points
50 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You 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);
    }
}
answered Nov 6, 2018 by Omkar
• 65,820 points

Related Questions In Blockchain

0 votes
1 answer

Hyperledger Composer rest server NPM versions

The Hyperledger Composer pre-requisites can be installed ...READ MORE

answered Jul 13, 2018 in Blockchain by Christine
• 15,790 points
55 views
0 votes
1 answer

Why hyperledger REST server has PUT method?

Actually, with the PUT method, you don't ...READ MORE

answered Jul 19, 2018 in Blockchain by ariaholic
• 7,320 points
12 views
0 votes
1 answer
+1 vote
1 answer

I am unable to change port of composer-rest-server. Please help!

Use full format for running a business ...READ MORE

answered Mar 27, 2018 in Blockchain by Christine
• 15,790 points
206 views
+1 vote
1 answer
0 votes
1 answer

HyperLedger Composer: Failed to load connector module “composer-connector-embedded” for connection profile “testprofile”

First, update your devDependencies in package.json "devDependencies": { ... "composer-admin": ...READ MORE

answered Nov 2, 2018 in Blockchain by Omkar
• 65,820 points
53 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.