improved error handling

Former-commit-id: f5086f9ebae6dab987fedf5e87d885c243af188e
This commit is contained in:
Jeremy Long
2014-04-11 06:38:13 -04:00
parent f23da0dd5a
commit 1ff45c8e02

View File

@@ -21,7 +21,6 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@@ -102,8 +101,7 @@ public class NexusSearch {
// 2) Otherwise, don't use the proxy (either the proxy isn't configured, // 2) Otherwise, don't use the proxy (either the proxy isn't configured,
// or proxy is specifically // or proxy is specifically
// set to false // set to false
URLConnection conn = null; final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
conn.setDoOutput(true); conn.setDoOutput(true);
@@ -112,36 +110,40 @@ public class NexusSearch {
conn.addRequestProperty("Accept", "application/xml"); conn.addRequestProperty("Accept", "application/xml");
conn.connect(); conn.connect();
try { if (conn.getResponseCode() == 200) {
final DocumentBuilder builder = DocumentBuilderFactory try {
.newInstance().newDocumentBuilder(); final DocumentBuilder builder = DocumentBuilderFactory
final Document doc = builder.parse(conn.getInputStream()); .newInstance().newDocumentBuilder();
final XPath xpath = XPathFactory.newInstance().newXPath(); final Document doc = builder.parse(conn.getInputStream());
final String groupId = xpath final XPath xpath = XPathFactory.newInstance().newXPath();
.evaluate( final String groupId = xpath
"/org.sonatype.nexus.rest.model.NexusArtifact/groupId", .evaluate(
doc); "/org.sonatype.nexus.rest.model.NexusArtifact/groupId",
final String artifactId = xpath.evaluate( doc);
"/org.sonatype.nexus.rest.model.NexusArtifact/artifactId", final String artifactId = xpath.evaluate(
doc); "/org.sonatype.nexus.rest.model.NexusArtifact/artifactId",
final String version = xpath doc);
.evaluate( final String version = xpath
"/org.sonatype.nexus.rest.model.NexusArtifact/version", .evaluate(
doc); "/org.sonatype.nexus.rest.model.NexusArtifact/version",
final String link = xpath doc);
.evaluate( final String link = xpath
"/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink", .evaluate(
doc); "/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink",
return new MavenArtifact(groupId, artifactId, version, link); doc);
} catch (FileNotFoundException fnfe) { return new MavenArtifact(groupId, artifactId, version, link);
/* This is what we get when the SHA1 they sent doesn't exist in } catch (Throwable e) {
* Nexus. This is useful upstream for recovery, so we just re-throw it // Anything else is jacked-up XML stuff that we really can't recover
*/ // from well
throw fnfe; throw new IOException(e.getMessage(), e);
} catch (Throwable e) { }
// Anything else is jacked-up XML stuff that we really can't recover } else if (conn.getResponseCode() == 404) {
// from well throw new FileNotFoundException("Artifact not found in Nexus");
throw new IOException(e.getMessage(), e); } else {
final String msg = String.format("Could not connect to Nexus received response code: %d %s",
conn.getResponseCode(), conn.getResponseMessage());
LOGGER.fine(msg);
throw new IOException(msg);
} }
} }