'Why doesn't sawtooth allow me to raise exceptions?

When I want to declare an error in my handler, it causes an infinite loop in my processor which displays in the console :

{ InvalidTransaction: InvalidTransaction: The campaign does not exist!
    at VoteHandler.apply (/app/handler.js:79:19)
    at <anonymous> name: 'InvalidTransaction' }

"The campaign does not exist!" being my error message.

Example :

const { TransactionHandler } = require('sawtooth-sdk/processor/handler');
const { InvalidTransaction } = require('sawtooth-sdk/processor/exceptions');

class VoteHandler extends TransactionHandler {
    constructor() {
        super(FAMILY_NAME, ['1.0'], [NAMESPACE]);
    }

    // apply function - execution starts here when a transaction reaches the validator for this transaction processsor
    async apply(batches, context) {
        var request = JSON.parse(decoder.decode(batches.payload));

        let campaign = {};
        
        let address = NAMESPACE + hash(request.idCampaign).substring(0, 14) + hash(batches.header.signerPublicKey).substring(0, 50);

        switch(request.type){
            case "INITIALIZE":
                await context.getState([address]).then((addressValues) => {
                    if(addressValues[address].length == 0){
                        console.log("Create the campaign!");
                    }else{
                        throw new InvalidTransaction("The campaign already exists!");
                    }
                }).catch((err) => {
                    throw new InvalidTransaction("Error when initializing the campaign!");
                });

                break;
            case "NEW_VOTE":
                await context.getState([address]).then((addressValues) => {
                    if(addressValues[address].length == 0){
                        throw new InvalidTransaction("The campaign doesn't exist, you can't vote!");
                    }else{
                        console.log("Vote taken into account!");
                    }
                }).catch((err) => {
                    throw new InvalidTransaction("The campaign does not exist!");
                });

                break;
            default:
                throw new InvalidTransaction("The type of action is not defined!");
        }

        console.log('==============State==============');
        console.log(campaign);
        console.log('=============Message=============');
        console.log(request);
        console.log('=================================');

        if(Object.keys(campaign).length !== 0){
            return context.setState({
                [address]: encoder.encode(JSON.stringify(campaign))
            });
        }
    }
}

I develop my processor in JS and I could see code examples in java, js, python, ... They always do the same thing, that is to say simply declare the exception by importing it from the sawtooth-sdk package (sawtooth-sdk/processor/exceptions).

Java :

throw new InvalidTransactionException("Version does not match");

LINK : Daml Transaction Java

Python :

raise InvalidTransaction('Invalid action: Take requires an existing game')

LINK : Sawtooth xo pyton

JS :

throw new InternalError('State Error!')

LINK : Simple Wallet JS

I also tried to get the exceptions from the package @restroom-mw/sawtooth-sdk/ but the error is still there. IMAGE : Import exceptions

Normally my error report should return an error code with my error message to my client.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source