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

fix(artifacts): improve github artifact downloader error handling #2319

Merged
merged 1 commit into from
Jan 26, 2018
Merged
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 @@ -27,12 +27,11 @@
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Request.Builder;
import com.squareup.okhttp.Response;
import lombok.extern.slf4j.Slf4j;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -93,18 +92,40 @@ private String credentialsFromFile(String filename) {

public InputStream download(Artifact artifact) throws IOException {
HttpUrl.Builder metadataUrlBuilder = HttpUrl.parse(artifact.getReference()).newBuilder();
metadataUrlBuilder.addQueryParameter("ref", artifact.getVersion());
String version = artifact.getVersion();
if (StringUtils.isEmpty(version)) {
log.info("No version specified for artifact {}, using 'master'.", version);
version = "master";
}

metadataUrlBuilder.addQueryParameter("ref", version);
Request metadataRequest = requestBuilder
.url(metadataUrlBuilder.build().toString())
.build();
Response metadataResponse = okHttpClient.newCall(metadataRequest).execute();

Response metadataResponse;
try {
metadataResponse = okHttpClient.newCall(metadataRequest).execute();
} catch (IOException e) {
throw new FailedDownloadException("Unable to determine the download URL of artifact " + artifact + ": " + e.getMessage(), e);

Choose a reason for hiding this comment

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

What's the Artifact.toString() look like? Will it be a nice human readable representation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry I missed your review:

It's the json expansion of the artifact (I think you've seen it now based on your slack messages).

}

String body = metadataResponse.body().string();
ContentMetadata metadata = objectMapper.readValue(body, ContentMetadata.class);
if (StringUtils.isEmpty(metadata.downloadUrl)) {
throw new FailedDownloadException("Failed to retrieve your github artifact's download URL. This is likely due to incorrect auth setup. Artifact: " + artifact);
}

Request downloadRequest = requestBuilder
.url(metadata.getDownloadUrl())
.build();
Response downloadResponse = okHttpClient.newCall(downloadRequest).execute();
return downloadResponse.body().byteStream();

try {
Response downloadResponse = okHttpClient.newCall(downloadRequest).execute();
return downloadResponse.body().byteStream();
} catch (IOException e) {
throw new FailedDownloadException("Unable to download the contents of artifact " + artifact + ": " + e.getMessage(), e);
}
}

@Override
Expand All @@ -117,4 +138,14 @@ public static class ContentMetadata {
@JsonProperty("download_url")
private String downloadUrl;
}

public class FailedDownloadException extends IOException {
public FailedDownloadException(String message) {
super(message);
}

public FailedDownloadException(String message, Throwable cause) {
super(message, cause);
}
}
}