improved error handling

Former-commit-id: a95f11da123ca42e4abe3e4b2a98f8cc44fc34e5
This commit is contained in:
Jeremy Long
2014-04-11 06:38:13 -04:00
parent ac2231f0f3
commit 711d8c8c6b

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,6 +110,7 @@ public class NexusSearch {
conn.addRequestProperty("Accept", "application/xml"); conn.addRequestProperty("Accept", "application/xml");
conn.connect(); conn.connect();
if (conn.getResponseCode() == 200) {
try { try {
final DocumentBuilder builder = DocumentBuilderFactory final DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder(); .newInstance().newDocumentBuilder();
@@ -133,16 +132,19 @@ public class NexusSearch {
"/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink", "/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink",
doc); doc);
return new MavenArtifact(groupId, artifactId, version, link); return new MavenArtifact(groupId, artifactId, version, link);
} catch (FileNotFoundException fnfe) {
/* This is what we get when the SHA1 they sent doesn't exist in
* Nexus. This is useful upstream for recovery, so we just re-throw it
*/
throw fnfe;
} catch (Throwable e) { } catch (Throwable e) {
// Anything else is jacked-up XML stuff that we really can't recover // Anything else is jacked-up XML stuff that we really can't recover
// from well // from well
throw new IOException(e.getMessage(), e); throw new IOException(e.getMessage(), e);
} }
} else if (conn.getResponseCode() == 404) {
throw new FileNotFoundException("Artifact not found in Nexus");
} 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);
}
} }
/** /**