public static ErrorCodes getErrorCode(ResponseException responseException)
      throws ResponseException {
    // Obtain the error response code.
    String errorContent = responseException.getContent();
    if (errorContent == null) {
      throw responseException;
    }

    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 responseException;
  }
Extract an {@link ErrorCodes} response from the error object encoded in an {@link
HttpResponseException}.

@param responseException the response exception
@return the parsed {@link ErrorCodes} if found
@throws ResponseException 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.
jib-core/src/main/java/com/google/cloud/tools/jib/registry/ErrorResponseUtil.java