Skip to content

Commit

Permalink
FABN-1566 Add TransactionError with event status (#258)
Browse files Browse the repository at this point in the history
When a transaction completes with a non-valid status
the error object will be of type TransactionError which
will include the transactionCode and transactionId attribute.

Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob authored Jun 25, 2020
1 parent 5b409b2 commit e71b126
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
35 changes: 35 additions & 0 deletions fabric-network/src/errors/transactionerror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2020 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

import { FabricError } from './fabricerror';

export interface TransactionErrorInfo {
message: string;
transactionId: string;
transactionCode: string;
}

/**
* Base type for Fabric-specific errors.
* @memberof module:fabric-network
* @property {string} [transactionId] ID of the associated transaction.
* @property {string} [transactionCode] The transaction validation code of the associated transaction.
*/
export class TransactionError extends FabricError {
transactionCode?: string;

/*
* Constructor.
* @param {(string|object)} [info] Either an error message (string) or additional properties to assign to this
* instance (object).
*/
constructor(info?: string | TransactionErrorInfo) {
super(info);
this.name = TransactionError.name;
}
}
7 changes: 6 additions & 1 deletion fabric-network/src/impl/event/transactioneventhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TransactionEventStrategy } from './transactioneventstrategy';
import { Network } from '../../network';
import { Endorser } from 'fabric-common';
import { CommitError, CommitEvent, CommitListener } from '../../events';
import { TransactionError } from '../../errors/transactionerror';

import * as Logger from '../../logger';
const logger = Logger.getLogger('TransactionEventHandler');
Expand Down Expand Up @@ -121,7 +122,11 @@ export class TransactionEventHandler implements TxEventHandler {
private eventCallback(error?: CommitError, event?: CommitEvent) {
if (event && !event.isValid) {
const message = `Commit of transaction ${this.transactionId} failed on peer ${event.peer.name} with status ${event.status}`;
this.strategyFail(new Error(message));
this.strategyFail(new TransactionError({
message,
transactionId: event.transactionId,
transactionCode: event.status
}));
}

const peer = error?.peer || event!.peer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ describe('TransactionEventHandler', () => {

await expect(handler.waitForEvents()).to.be.rejectedWith(invalidEventInfo.status);
});

it('fails when receiving an invalid event from peer', async () => {
await handler.startListening();
eventService.sendEvent(invalidEventInfo);

await expect(handler.waitForEvents()).to.be.rejectedWith(invalidEventInfo.status);
});
});

describe('timeouts', () => {
Expand Down Expand Up @@ -304,5 +311,4 @@ describe('TransactionEventHandler', () => {
}
});
});

});
1 change: 1 addition & 0 deletions fabric-network/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { X509Identity } from '../lib/impl/wallet/x509identity';
export * from '../lib/events';
export { FabricError } from '../lib/errors/fabricerror';
export { TimeoutError } from '../lib/errors/timeouterror';
export { TransactionError, TransactionErrorInfo } from '../lib/errors/transactionerror';
export { QueryHandlerFactory };
export { QueryHandler } from '../lib/impl/query/queryhandler';
export { Query, QueryResults, QueryResponse } from '../lib/impl/query/query';
Expand Down

0 comments on commit e71b126

Please sign in to comment.