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