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

Create helper for parsing an error code from error response #938

Merged
merged 7 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -23,10 +23,6 @@
import com.google.cloud.tools.jib.http.BlobHttpContent;
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.image.DescriptorDigest;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate;
import com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -71,34 +67,15 @@ public BlobDescriptor handleHttpResponseException(HttpResponseException httpResp
}

// Finds a BLOB_UNKNOWN error response code.
String errorContent = httpResponseException.getContent();
if (errorContent == null) {
if (httpResponseException.getContent() == null) {
// TODO: The Google HTTP client gives null content for HEAD requests. Make the content never
// be null, even for HEAD requests.
return null;
}

} else {
try {
ErrorResponseTemplate errorResponse =
JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class);
List<ErrorEntryTemplate> errors = errorResponse.getErrors();
if (errors.size() == 1) {
String errorCodeString = errors.get(0).getCode();
if (errorCodeString == null) {
// Did not get an error code back.
throw httpResponseException;
}
ErrorCodes errorCode = ErrorCodes.valueOf(errorCodeString);
if (errorCode.equals(ErrorCodes.BLOB_UNKNOWN)) {
return null;
}
}

} catch (IOException ex) {
throw new RegistryErrorExceptionBuilder(getActionDescription(), ex)
.addReason("Failed to parse registry error response body")
.build();
}
ErrorCodes errorCode = ErrorResponseUtil.getErrorCode(httpResponseException);
if (errorCode == ErrorCodes.BLOB_UNKNOWN) {
return null;
}

// BLOB_UNKNOWN was not found as a error response code.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.tools.jib.registry;

import com.google.api.client.http.HttpResponseException;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate;
import com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate;
import java.io.IOException;
import java.util.List;

/** Utility methods for parsing {@link ErrorResponseTemplate JSON-encoded error responses}. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the JSON-encoded error responses intended to be inside the {@link?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Callers aren't necessarily aware of the class, but its javadocs have a nice description of the object layout.

public class ErrorResponseUtil {

/**
* Extract an {@link ErrorCodes} response from the error object encoded in an {@link
* HttpResponseException}.
*
* @param httpResponseException the response exception
* @return the parsed {@link ErrorCodes} if found
* @throws HttpResponseException rethrows the original exception if an error object could not be
* parsed, if there were multiple error objects, or if the error code is unknown.
*/
public static ErrorCodes getErrorCode(HttpResponseException httpResponseException)
throws HttpResponseException {
// Obtain the error response code.
String errorContent = httpResponseException.getContent();
if (errorContent == null) {
throw httpResponseException;
}

try {
ErrorResponseTemplate errorResponse =
JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class);
List<ErrorEntryTemplate> errors = errorResponse.getErrors();
// There may be multiple error objects
if (errors.size() == 1) {
String errorCodeString = errors.get(0).getCode();
// May not get an error code back.
if (errorCodeString != null) {
// throws IllegalArgumentException if unknown error code
return ErrorCodes.valueOf(errorCodeString);
}
}

} catch (IOException | IllegalArgumentException ex) {
// Parse exception: either isn't an error object or unknown error code
}

// rethrow the original exception
throw httpResponseException;
}

// not intended to be instantiated
private ErrorResponseUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import com.google.cloud.tools.jib.http.Response;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate;
import com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
Expand Down Expand Up @@ -77,40 +74,13 @@ public Void handleHttpResponseException(HttpResponseException httpResponseExcept
throw httpResponseException;
}

// TODO turn deserializing the error-code into a library method

// Obtain the error response code.
String errorContent = httpResponseException.getContent();
if (errorContent == null) {
throw httpResponseException;
}

try {
ErrorResponseTemplate errorResponse =
JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class);
List<ErrorEntryTemplate> errors = errorResponse.getErrors();
if (errors.size() == 1) {
String errorCodeString = errors.get(0).getCode();
if (errorCodeString == null) {
// Did not get an error code back.
throw httpResponseException;
}
ErrorCodes errorCode = ErrorCodes.valueOf(errorCodeString);
if (errorCode.equals(ErrorCodes.MANIFEST_INVALID)
|| errorCode.equals(ErrorCodes.TAG_INVALID)) {
throw new RegistryErrorExceptionBuilder(getActionDescription(), httpResponseException)
.addReason("Registry may not support Image Manifest Version 2, Schema 2")
.build();
}
}

} catch (IOException ex) {
ErrorCodes errorCode = ErrorResponseUtil.getErrorCode(httpResponseException);
if (errorCode == ErrorCodes.MANIFEST_INVALID || errorCode == ErrorCodes.TAG_INVALID) {
throw new RegistryErrorExceptionBuilder(getActionDescription(), httpResponseException)
.addReason("Failed to parse registry error response body")
.addReason("Registry may not support Image Manifest Version 2, Schema 2")
.build();
}

// unhandled error response code.
// rethrow: unhandled error response code.
throw httpResponseException;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.tools.jib.registry;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponseException;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.junit.Test;

/** Test for {@link ErrorReponseUtil}. */
public class ErrorResponseUtilTest {

@Test
public void testGetErrorCode_knownErrorCode() throws HttpResponseException {
HttpResponseException httpResponseException =
new HttpResponseException.Builder(
HttpStatus.SC_BAD_REQUEST, "Bad Request", new HttpHeaders())
.setContent(
"{\"errors\":[{\"code\":\"MANIFEST_INVALID\",\"message\":\"manifest invalid\",\"detail\":{}}]}")
.build();

Assert.assertSame(
ErrorCodes.MANIFEST_INVALID, ErrorResponseUtil.getErrorCode(httpResponseException));
}

/** An unknown {@link ErrorCodes} should cause original exception to be rethrown. */
@Test
public void testGetErrorCode_unknownErrorCode() {
HttpResponseException httpResponseException =
new HttpResponseException.Builder(
HttpStatus.SC_BAD_REQUEST, "Bad Request", new HttpHeaders())
.setContent(
"{\"errors\":[{\"code\":\"INVALID_ERROR_CODE\",\"message\":\"invalid code\",\"detail\":{}}]}")
.build();
try {
ErrorResponseUtil.getErrorCode(httpResponseException);
Assert.fail();
} catch (HttpResponseException ex) {
Assert.assertSame(httpResponseException, ex);
}
}

/** Multiple error objects should cause original exception to be rethrown. */
@Test
public void testGetErrorCode_multipleErrors() {
HttpResponseException httpResponseException =
new HttpResponseException.Builder(
HttpStatus.SC_BAD_REQUEST, "Bad Request", new HttpHeaders())
.setContent(
"{\"errors\":["
+ "{\"code\":\"MANIFEST_INVALID\",\"message\":\"message 1\",\"detail\":{}},"
+ "{\"code\":\"TAG_INVALID\",\"message\":\"message 2\",\"detail\":{}}"
+ "]}")
.build();
try {
ErrorResponseUtil.getErrorCode(httpResponseException);
Assert.fail();
} catch (HttpResponseException ex) {
Assert.assertSame(httpResponseException, ex);
}
}

/** An non-error object should cause original exception to be rethrown. */
@Test
public void testGetErrorCode_invalidErrorObject() {
HttpResponseException httpResponseException =
new HttpResponseException.Builder(
HttpStatus.SC_BAD_REQUEST, "Bad Request", new HttpHeaders())
.setContent("{\"type\":\"other\",\"message\":\"some other object\"}")
.build();
try {
ErrorResponseUtil.getErrorCode(httpResponseException);
Assert.fail();
} catch (HttpResponseException ex) {
Assert.assertSame(httpResponseException, ex);
}
}
}