Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved : NPE if the server doesn't provide any HTTP content for an error #2542

Merged
merged 7 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
class RegistryEndpointCaller<T> {

/**
* <a
* href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308">https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308</a>.
* <a href = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308">https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308</a>.
*/
@VisibleForTesting static final int STATUS_CODE_PERMANENT_REDIRECT = 308;

Expand Down Expand Up @@ -188,22 +187,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");
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
}

return registryErrorExceptionBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down