diff --git a/gocd-file-based-secrets-plugin/src/main/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutor.java b/gocd-file-based-secrets-plugin/src/main/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutor.java index f4a1220..01ef51e 100644 --- a/gocd-file-based-secrets-plugin/src/main/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutor.java +++ b/gocd-file-based-secrets-plugin/src/main/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutor.java @@ -60,7 +60,8 @@ protected GoPluginApiResponse execute(LookupSecretRequest lookupSecretsRequest) Map response = Collections.singletonMap("message", String.format("Secrets with keys %s not found.", unresolvedKeys)); return new DefaultGoPluginApiResponse(NOT_FOUND_ERROR_CODE, GSON.toJson(response)); } catch (IOException | GeneralSecurityException | BadSecretException e) { - return DefaultGoPluginApiResponse.error("Error while looking up secrets: " + e.getMessage()); + Map errorMessage = Collections.singletonMap("message", "Error while looking up secrets: " + e.getMessage()); + return DefaultGoPluginApiResponse.error(GSON.toJson(errorMessage)); } } diff --git a/gocd-file-based-secrets-plugin/src/test/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutorTest.java b/gocd-file-based-secrets-plugin/src/test/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutorTest.java index 1df328f..866ce85 100644 --- a/gocd-file-based-secrets-plugin/src/test/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutorTest.java +++ b/gocd-file-based-secrets-plugin/src/test/java/cd/go/plugin/secret/filebased/executors/LookupSecretsRequestExecutorTest.java @@ -24,7 +24,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.skyscreamer.jsonassert.JSONAssert; import java.io.File; import java.io.IOException; @@ -39,6 +38,7 @@ import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; class LookupSecretsRequestExecutorTest { + private File databaseFile; @BeforeEach @@ -87,4 +87,15 @@ void shouldReturnEmptyResponseWhenSecretsAreNotPresent() throws JSONException { assertEquals("{\"message\":\"Secrets with keys [randomKey1, randomKey2] not found.\"}", response.responseBody(), true); } + @Test + void shouldErrorAsAMapWhenAnyExceptionOccurs() throws JSONException { + GoPluginApiRequest goPluginApiRequest = mock(GoPluginApiRequest.class); + when(goPluginApiRequest.requestBody()).thenReturn( + new LookupSecretRequest("some-non-existent-file.db", Arrays.asList("randomKey1", "randomKey2")).toJSON()); + + GoPluginApiResponse response = new LookupSecretsRequestExecutor().execute(goPluginApiRequest); + + assertThat(response.responseCode()).isEqualTo(500); + assertEquals("{\"message\":\"Error while looking up secrets: File \\u0027some-non-existent-file.db\\u0027 does not exist\"}", response.responseBody(), true); + } }