Skip to content

Commit 00536fb

Browse files
committed
Create new GraffitiManagementException
1 parent 05fad61 commit 00536fb

File tree

7 files changed

+65
-24
lines changed

7 files changed

+65
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright Consensys Software Inc., 2024
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package tech.pegasys.teku.validator.api;
15+
16+
import java.io.IOException;
17+
18+
public class GraffitiManagementException extends IOException {
19+
20+
public GraffitiManagementException(final String message) {
21+
super(message);
22+
}
23+
24+
public GraffitiManagementException(final String message, final Throwable cause) {
25+
super(message, cause);
26+
}
27+
}

validator/api/src/main/java/tech/pegasys/teku/validator/api/GraffitiManager.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public GraffitiManager(final Path directory) {
4040
}
4141
}
4242

43-
public void setGraffiti(final BLSPublicKey publicKey, final String graffiti) throws IOException {
43+
public void setGraffiti(final BLSPublicKey publicKey, final String graffiti)
44+
throws GraffitiManagementException {
4445
final String strippedGraffiti = graffiti.strip();
4546
final int graffitiSize = strippedGraffiti.getBytes(StandardCharsets.UTF_8).length;
4647
if (graffitiSize > 32) {
@@ -50,17 +51,27 @@ public void setGraffiti(final BLSPublicKey publicKey, final String graffiti) thr
5051
strippedGraffiti, graffitiSize));
5152
}
5253

53-
final Path file = directory.resolve(resolveFileName(publicKey));
54-
Files.writeString(file, strippedGraffiti);
54+
try {
55+
final Path file = directory.resolve(resolveFileName(publicKey));
56+
Files.writeString(file, strippedGraffiti);
57+
} catch (IOException e) {
58+
throw new GraffitiManagementException(
59+
"Unable to update graffiti for validator " + publicKey, e);
60+
}
5561
}
5662

57-
public void deleteGraffiti(final BLSPublicKey publicKey) throws IOException {
63+
public void deleteGraffiti(final BLSPublicKey publicKey) throws GraffitiManagementException {
5864
final Path file = directory.resolve(resolveFileName(publicKey));
5965
if (!file.toFile().exists()) {
6066
return;
6167
}
6268

63-
Files.delete(file);
69+
try {
70+
Files.delete(file);
71+
} catch (IOException e) {
72+
throw new GraffitiManagementException(
73+
"Unable to delete graffiti for validator " + publicKey, e);
74+
}
6475
}
6576

6677
public Optional<Bytes32> getGraffiti(final BLSPublicKey publicKey) {

validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ void deleteGraffiti_shouldReturnErrorMessageWhenUnableToDeleteFile(@TempDir fina
138138
assertThat(file.createNewFile()).isTrue();
139139
assertThat(file.getParentFile().setWritable(false)).isTrue();
140140

141-
assertThatThrownBy(() -> manager.deleteGraffiti(publicKey)).isInstanceOf(IOException.class);
141+
assertThatThrownBy(() -> manager.deleteGraffiti(publicKey))
142+
.isInstanceOf(GraffitiManagementException.class);
142143
assertThat(file.exists()).isTrue();
143144
}
144145

validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffiti.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import static tech.pegasys.teku.validator.client.restapi.ValidatorTypes.PARAM_PUBKEY_TYPE;
2121

2222
import com.fasterxml.jackson.core.JsonProcessingException;
23-
import java.io.IOException;
2423
import java.util.Optional;
2524
import tech.pegasys.teku.bls.BLSPublicKey;
2625
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata;
2726
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
2827
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
28+
import tech.pegasys.teku.validator.api.GraffitiManagementException;
2929
import tech.pegasys.teku.validator.api.GraffitiManager;
3030
import tech.pegasys.teku.validator.client.KeyManager;
3131
import tech.pegasys.teku.validator.client.Validator;
@@ -66,9 +66,8 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
6666
try {
6767
graffitiManager.deleteGraffiti(publicKey);
6868
request.respondWithCode(SC_NO_CONTENT);
69-
} catch (IOException e) {
70-
request.respondError(
71-
SC_INTERNAL_SERVER_ERROR, "Unable to delete graffiti for validator " + publicKey);
69+
} catch (GraffitiManagementException e) {
70+
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
7271
}
7372
}
7473
}

validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffiti.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
import static tech.pegasys.teku.validator.client.restapi.ValidatorTypes.PARAM_PUBKEY_TYPE;
2222

2323
import com.fasterxml.jackson.core.JsonProcessingException;
24-
import java.io.IOException;
2524
import java.util.Optional;
2625
import java.util.function.Function;
2726
import tech.pegasys.teku.bls.BLSPublicKey;
2827
import tech.pegasys.teku.infrastructure.json.types.DeserializableTypeDefinition;
2928
import tech.pegasys.teku.infrastructure.restapi.endpoints.EndpointMetadata;
3029
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
3130
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
31+
import tech.pegasys.teku.validator.api.GraffitiManagementException;
3232
import tech.pegasys.teku.validator.api.GraffitiManager;
3333
import tech.pegasys.teku.validator.client.KeyManager;
3434
import tech.pegasys.teku.validator.client.Validator;
@@ -81,9 +81,8 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
8181
try {
8282
graffitiManager.setGraffiti(publicKey, graffiti);
8383
request.respondWithCode(SC_NO_CONTENT);
84-
} catch (IOException e) {
85-
request.respondError(
86-
SC_INTERNAL_SERVER_ERROR, "Unable to update graffiti for validator " + publicKey);
84+
} catch (GraffitiManagementException e) {
85+
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
8786
}
8887
}
8988
}

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest;
4141
import tech.pegasys.teku.spec.TestSpecFactory;
4242
import tech.pegasys.teku.spec.util.DataStructureUtil;
43+
import tech.pegasys.teku.validator.api.GraffitiManagementException;
4344
import tech.pegasys.teku.validator.api.GraffitiManager;
4445
import tech.pegasys.teku.validator.client.OwnedKeyManager;
4546
import tech.pegasys.teku.validator.client.Validator;
@@ -71,19 +72,20 @@ void shouldSuccessfullyDeleteGraffiti() throws IOException {
7172
}
7273

7374
@Test
74-
void shouldReturnErrorWhenIssueDeleting() throws IOException {
75+
void shouldReturnErrorWhenIssueDeletingGraffiti() throws IOException {
76+
final String errorMessage = "Unable to delete graffiti for validator " + publicKey;
7577
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
7678
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
77-
doThrow(IOException.class).when(graffitiManager).deleteGraffiti(any());
79+
doThrow(new GraffitiManagementException(errorMessage))
80+
.when(graffitiManager)
81+
.deleteGraffiti(any());
7882

7983
handler.handleRequest(request);
8084

8185
verify(graffitiManager).deleteGraffiti(eq(publicKey));
8286
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
8387
assertThat(request.getResponseBody())
84-
.isEqualTo(
85-
new HttpErrorResponse(
86-
SC_INTERNAL_SERVER_ERROR, "Unable to delete graffiti for validator " + publicKey));
88+
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, errorMessage));
8789
}
8890

8991
@Test

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import tech.pegasys.teku.infrastructure.restapi.StubRestApiRequest;
4242
import tech.pegasys.teku.spec.TestSpecFactory;
4343
import tech.pegasys.teku.spec.util.DataStructureUtil;
44+
import tech.pegasys.teku.validator.api.GraffitiManagementException;
4445
import tech.pegasys.teku.validator.api.GraffitiManager;
4546
import tech.pegasys.teku.validator.client.OwnedKeyManager;
4647
import tech.pegasys.teku.validator.client.Validator;
@@ -75,21 +76,22 @@ void shouldSuccessfullySetGraffiti() throws IOException {
7576
}
7677

7778
@Test
78-
void shouldReturnErrorWhenIssueSetting() throws IOException {
79+
void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException {
80+
final String errorMessage = "Unable to update graffiti for validator " + publicKey;
7981
request.setRequestBody(graffiti);
8082

8183
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
8284
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
83-
doThrow(IOException.class).when(graffitiManager).setGraffiti(any(), eq(graffiti));
85+
doThrow(new GraffitiManagementException(errorMessage))
86+
.when(graffitiManager)
87+
.setGraffiti(any(), eq(graffiti));
8488

8589
handler.handleRequest(request);
8690

8791
verify(graffitiManager).setGraffiti(eq(publicKey), eq(graffiti));
8892
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
8993
assertThat(request.getResponseBody())
90-
.isEqualTo(
91-
new HttpErrorResponse(
92-
SC_INTERNAL_SERVER_ERROR, "Unable to update graffiti for validator " + publicKey));
94+
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, errorMessage));
9395
}
9496

9597
@Test

0 commit comments

Comments
 (0)