Skip to content

Commit

Permalink
RPC parameter error improvements. Test updates. (#4510)
Browse files Browse the repository at this point in the history
* RPC parameter error improvements. Test updates.

Signed-off-by: mark-terry <[email protected]>
  • Loading branch information
mark-terry authored Oct 11, 2022
1 parent c9d4b89 commit 7a2db24
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Additions and Improvements
- Improved RLP processing of zero-length string as 0x80 [#4283](https://github.com/hyperledger/besu/pull/4283) [#4388](https://github.com/hyperledger/besu/issues/4388)
- Increased level of detail in JSON-RPC parameter error log messages [#4510](https://github.com/hyperledger/besu/pull/4510)

### Bug Fixes
- Corrects emission of blockadded events when rewinding during a re-org. Fix for [#4495](https://github.com/hyperledger/besu/issues/4495)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void returnsCorrectMethodName() {
public void exceptionWhenInvalidStartBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
public void exceptionWhenInvalidEndBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("1", "INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void exceptionWhenNoParamsSupplied() {
public void exceptionWhenInvalidAddressParameterSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("InvalidAddress")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void returnsCorrectMethodName() {
public void exceptionWhenInvalidStartBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
public void exceptionWhenInvalidEndBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("1", "INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public void exceptionWhenNoAuthSupplied() {
public void exceptionWhenNoAddressSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("true")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
public void exceptionWhenInvalidBoolParameterSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams(Address.fromHexString("1"), "c")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void exceptionWhenNoParamsSupplied() {
public void exceptionWhenInvalidAddressParameterSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("InvalidAddress")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void returnsCorrectMethodName() {
public void exceptionWhenInvalidStartBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
public void exceptionWhenInvalidEndBlockSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("1", "INVALID")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public void exceptionWhenNoAuthSupplied() {
public void exceptionWhenNoAddressSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams("true")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
public void exceptionWhenInvalidBoolParameterSupplied() {
assertThatThrownBy(() -> method.response(requestWithParams(Address.fromHexString("1"), "c")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public void missingTagParameterBlockHashes() {

assertThat(thrown)
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

/** The Tag | Quantity is the first parameter, either a String or a number */
Expand All @@ -422,7 +422,7 @@ public void missingTagParameterBlockTransactions() {

assertThat(thrown)
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public <T> Optional<T> optional(
final String json = mapper.writeValueAsString(rawParam);
param = mapper.readValue(json, paramClass);
} catch (final JsonProcessingException e) {
throw new InvalidJsonRpcParameters("Invalid json rpc parameter at index " + index, e);
throw new InvalidJsonRpcParameters(
String.format(
"Invalid json rpc parameter at index %d. Supplied value was: '%s' of type: '%s' - expected type: '%s'",
index, rawParam, rawParam.getClass().getName(), paramClass.getName()),
e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ public void exceptionWhenNoBoolSupplied() {
public void exceptionWhenHashParamInvalid() {
assertThatThrownBy(() -> method.response(requestWithParams("hash", "true")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessage(
"Invalid json rpc parameter at index 0. Supplied value was: 'hash' of type: 'java.lang.String' - expected type: 'org.hyperledger.besu.datatypes.Hash'");
verifyNoMoreInteractions(blockchainQueries);
}

@Test
public void exceptionWhenBoolParamInvalid() {
assertThatThrownBy(() -> method.response(requestWithParams(ZERO_HASH, "maybe")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessage(
"Invalid json rpc parameter at index 1. Supplied value was: 'maybe' of type: 'java.lang.String' - expected type: 'java.lang.Boolean'");
verifyNoMoreInteractions(blockchainQueries);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ public void exceptionWhenNoBoolSupplied() {
public void exceptionWhenNumberParamInvalid() {
assertThatThrownBy(() -> method.response(requestWithParams("invalid", "true")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessage(
"Invalid json rpc parameter at index 0. Supplied value was: 'invalid' of type: 'java.lang.String' - expected type: 'org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter'");
verifyNoMoreInteractions(blockchainQueries);
}

@Test
public void exceptionWhenBoolParamInvalid() {
assertThatThrownBy(() -> method.response(requestWithParams("0", "maybe")))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessage(
"Invalid json rpc parameter at index 1. Supplied value was: 'maybe' of type: 'java.lang.String' - expected type: 'java.lang.Boolean'");
verifyNoMoreInteractions(blockchainQueries);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void exceptionWhenHashParamInvalidTest() {
JsonRpcRequestContext requestContext = new JsonRpcRequestContext(request);
assertThatThrownBy(() -> method.response(requestContext))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");

verifyNoMoreInteractions(blockchainQueries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void exceptionWhenNumberParamInvalidTest() {
JsonRpcRequestContext requestContext = new JsonRpcRequestContext(request);
assertThatThrownBy(() -> method.response(requestContext))
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");

verifyNoMoreInteractions(blockchainQueries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void shouldReturnErrorWhenInvalidBlockHashParam() {

assertThat(thrown)
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
Expand All @@ -113,7 +113,7 @@ public void shouldReturnErrorWhenInvalidIndexParam() {

assertThat(thrown)
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public String getDescription() {
catchThrowableOfType(
() -> privCreatePrivacyGroup.response(request), InvalidJsonRpcParameters.class);

assertThat(response.getMessage()).isEqualTo("Invalid json rpc parameter at index 0");
assertThat(response.getMessage()).contains("Invalid json rpc parameter at index 0");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void mapRequestToNewHeadsWithInvalidSecondParamFails() {
.isInstanceOf(InvalidSubscriptionRequestException.class)
.getCause()
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down Expand Up @@ -290,7 +290,7 @@ public void mapRequestToLogsWithInvalidTopicInFilter() {
.isInstanceOf(InvalidSubscriptionRequestException.class)
.getCause()
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand All @@ -303,7 +303,7 @@ public void mapRequestToLogsWithInvalidSecondParam() {
.isInstanceOf(InvalidSubscriptionRequestException.class)
.getCause()
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 1");
.hasMessageContaining("Invalid json rpc parameter at index 1");
}

@Test
Expand Down Expand Up @@ -372,7 +372,7 @@ public void mapAbsentSubscriptionTypeRequestFails() {
.isInstanceOf(InvalidSubscriptionRequestException.class)
.getCause()
.isInstanceOf(InvalidJsonRpcParameters.class)
.hasMessage("Invalid json rpc parameter at index 0");
.hasMessageContaining("Invalid json rpc parameter at index 0");
}

@Test
Expand Down

0 comments on commit 7a2db24

Please sign in to comment.