Source:
static DatastoreException makeException(String url, String methodName, InputStream content, String contentType, Charset contentCharset, Throwable cause, int httpStatusCode) { if (!contentType.equals("application/x-protobuf")) { String responseContent; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(content, out); responseContent = out.toString(contentCharset.name()); } catch (IOException e) { responseContent = ""; } return makeException(url, methodName, Code.INTERNAL, String.format( "Non-protobuf error: %s. HTTP status code was %d.", responseContent, httpStatusCode), cause); } Status rpcStatus; try { rpcStatus = Status.parseFrom(content); } catch (IOException e) { return makeException(url, methodName, Code.INTERNAL, String.format( "Unable to parse Status protocol buffer: HTTP status code was %s.", httpStatusCode), e); } finally { try { content.close(); } catch(IOException e) { logger.fine(String.format("closing content stream failed: %s",e)); } } Code code = Code.valueOf(rpcStatus.getCode()); if (code == null) { return makeException(url, methodName, Code.INTERNAL, String.format( "Invalid error code: %d. Message: %s.", rpcStatus.getCode(), rpcStatus.getMessage()), cause); } else if (code == Code.OK) { if (httpStatusCode == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) { return makeException(url, methodName, Code.UNAUTHENTICATED, "Unauthenticated.", cause); } return makeException(url, methodName, Code.INTERNAL, String.format("Unexpected OK error code with HTTP status code of %d. Message: %s.", httpStatusCode, rpcStatus.getMessage()), cause); } return makeException(url, methodName, code, rpcStatus.getMessage(), cause); }

Target:
static DatastoreException makeException(String url, String methodName, InputStream content, String contentType, Charset contentCharset, Throwable cause, int httpStatusCode) { if (!contentType.equals("application/x-protobuf")) { String responseContent; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(content, out, false); responseContent = out.toString(contentCharset.name()); } catch (IOException e) { responseContent = ""; } return makeException(url, methodName, Code.INTERNAL, String.format( "Non-protobuf error: %s. HTTP status code was %d.", responseContent, httpStatusCode), cause); } Status rpcStatus; try { rpcStatus = Status.parseFrom(content); } catch (IOException e) { return makeException(url, methodName, Code.INTERNAL, String.format( "Unable to parse Status protocol buffer: HTTP status code was %s.", httpStatusCode), e); } Code code = Code.valueOf(rpcStatus.getCode()); if (code == null) { return makeException(url, methodName, Code.INTERNAL, String.format( "Invalid error code: %d. Message: %s.", rpcStatus.getCode(), rpcStatus.getMessage()), cause); } else if (code == Code.OK) { if (httpStatusCode == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) { return makeException(url, methodName, Code.UNAUTHENTICATED, "Unauthenticated.", cause); } return makeException(url, methodName, Code.INTERNAL, String.format("Unexpected OK error code with HTTP status code of %d. Message: %s.", httpStatusCode, rpcStatus.getMessage()), cause); } return makeException(url, methodName, code, rpcStatus.getMessage(), cause); }