Skip to content

Commit 601eade

Browse files
committed
[FAB-16091] Handle invalid contract name
Change-Id: I4897e11686199edf4db7df974a59f2dcd20af132 Signed-off-by: James Taylor <[email protected]>
1 parent 614b14b commit 601eade

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/RoutingRegistryImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.hyperledger.fabric.Logger;
2020
import org.hyperledger.fabric.contract.ContractInterface;
21+
import org.hyperledger.fabric.contract.ContractRuntimeException;
2122
import org.hyperledger.fabric.contract.annotation.Contract;
2223
import org.hyperledger.fabric.contract.annotation.DataType;
2324
import org.hyperledger.fabric.contract.annotation.Transaction;
@@ -113,7 +114,13 @@ public TxFunction getTxFn(InvocationRequest request) {
113114
*/
114115
@Override
115116
public ContractDefinition getContract(String namespace) {
116-
return contracts.get(namespace);
117+
ContractDefinition contract = contracts.get(namespace);
118+
119+
if (contract == null) {
120+
throw new ContractRuntimeException("Undefined contract called");
121+
}
122+
123+
return contract;
117124
}
118125

119126
/*

fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,33 @@ public void testInvokeTxnWithDefinedNameUsingMethodName() {
156156
assertThat(SampleContract.t1Invoked, is(0));
157157
}
158158

159+
@Test
160+
public void testInvokeContractThatDoesNotExist() {
161+
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });
162+
r.findAllContracts();
163+
ChaincodeStub s = new ChaincodeStubNaiveImpl();
164+
165+
List<String> args = new ArrayList<>();
166+
args.add("thereisnocontract:t1");
167+
args.add("asdf");
168+
((ChaincodeStubNaiveImpl) s).setStringArgs(args);
169+
170+
SampleContract.beforeInvoked = 0;
171+
SampleContract.afterInvoked = 0;
172+
SampleContract.doWorkInvoked = 0;
173+
SampleContract.t1Invoked = 0;
174+
175+
Chaincode.Response response = r.invoke(s);
176+
assertThat(response, is(notNullValue()));
177+
assertThat(response.getStatus(), is(Chaincode.Response.Status.INTERNAL_SERVER_ERROR));
178+
assertThat(response.getMessage(), is(equalTo("Undefined contract called")));
179+
assertThat(response.getStringPayload(), is(nullValue()));
180+
assertThat(SampleContract.beforeInvoked, is(0));
181+
assertThat(SampleContract.afterInvoked, is(0));
182+
assertThat(SampleContract.doWorkInvoked, is(0));
183+
assertThat(SampleContract.t1Invoked, is(0));
184+
}
185+
159186
@Test
160187
public void testInvokeTxnThatDoesNotExist() {
161188
ContractRouter r = new ContractRouter(new String[] { "-a", "127.0.0.1:7052", "-i", "testId" });

0 commit comments

Comments
 (0)