diff --git a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryEndpointCaller.java b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryEndpointCaller.java index eb010f0075..d04c2cb5f8 100644 --- a/jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryEndpointCaller.java +++ b/jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryEndpointCaller.java @@ -46,8 +46,8 @@ class RegistryEndpointCaller { /** - * https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308. + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308. */ @VisibleForTesting static final int STATUS_CODE_PERMANENT_REDIRECT = 308; @@ -188,22 +188,28 @@ RegistryErrorException newRegistryErrorException(ResponseException responseExcep RegistryErrorExceptionBuilder registryErrorExceptionBuilder = new RegistryErrorExceptionBuilder( registryEndpointProvider.getActionDescription(), responseException); - - try { - ErrorResponseTemplate errorResponse = - JsonTemplateMapper.readJson(responseException.getContent(), ErrorResponseTemplate.class); - for (ErrorEntryTemplate errorEntry : errorResponse.getErrors()) { - registryErrorExceptionBuilder.addReason(errorEntry); + if (responseException.getContent() != null) { + try { + ErrorResponseTemplate errorResponse = + JsonTemplateMapper.readJson( + responseException.getContent(), ErrorResponseTemplate.class); + for (ErrorEntryTemplate errorEntry : errorResponse.getErrors()) { + registryErrorExceptionBuilder.addReason(errorEntry); + } + } catch (IOException ex) { + registryErrorExceptionBuilder.addReason( + "registry returned error code " + + responseException.getStatusCode() + + "; possible causes include invalid or wrong reference. Actual error output follows:\n" + + responseException.getContent() + + "\n"); } - } catch (IOException ex) { + } else { registryErrorExceptionBuilder.addReason( "registry returned error code " + responseException.getStatusCode() - + "; possible causes include invalid or wrong reference. Actual error output follows:\n" - + responseException.getContent() - + "\n"); + + " but did not return any details; possible causes include invalid or wrong reference, or proxy/firewall/VPN interfering \n"); } - return registryErrorExceptionBuilder.build(); } diff --git a/jib-core/src/test/java/com/google/cloud/tools/jib/registry/RegistryEndpointCallerTest.java b/jib-core/src/test/java/com/google/cloud/tools/jib/registry/RegistryEndpointCallerTest.java index 903198104f..afe384cb04 100644 --- a/jib-core/src/test/java/com/google/cloud/tools/jib/registry/RegistryEndpointCallerTest.java +++ b/jib-core/src/test/java/com/google/cloud/tools/jib/registry/RegistryEndpointCallerTest.java @@ -435,6 +435,24 @@ public void testNewRegistryErrorException_nonJsonErrorOutput() { registryException.getMessage()); } + @Test + public void testNewRegistryErrorException_noOutputFromRegistry() { + ResponseException httpException = Mockito.mock(ResponseException.class); + // Registry returning null error output + Mockito.when(httpException.getContent()).thenReturn(null); + Mockito.when(httpException.getStatusCode()).thenReturn(404); + + RegistryErrorException registryException = + endpointCaller.newRegistryErrorException(httpException); + Assert.assertSame(httpException, registryException.getCause()); + Assert.assertEquals( + "Tried to actionDescription but failed because: registry returned error code 404 " + + "but did not return any details; possible causes include invalid or wrong reference, or proxy/firewall/VPN interfering \n" + + " | If this is a bug, please file an issue at " + + "https://github.com/GoogleContainerTools/jib/issues/new", + registryException.getMessage()); + } + /** * Verifies that a response with {@code httpStatusCode} throws {@link * RegistryUnauthorizedException}.