diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-08-04 10:21:12 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-08-04 10:21:12 +0200 |
commit | dd7b6e304adcd0365ed568833142590ebcaf7118 (patch) | |
tree | 201ab7e88e0d9c315bc0e2d6ef5ced3d1005292e /sonar-ws-client | |
parent | b8ae0c40bc4d6dca25a745b01d1d93cbf8430264 (diff) | |
download | sonarqube-dd7b6e304adcd0365ed568833142590ebcaf7118.tar.gz sonarqube-dd7b6e304adcd0365ed568833142590ebcaf7118.zip |
SONAR-2667 Keep context when Java Web Service raises exceptions
Diffstat (limited to 'sonar-ws-client')
4 files changed, 120 insertions, 22 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java index 6426f58d78b..e6a2dc855b6 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java @@ -22,6 +22,7 @@ package org.sonar.wsclient; import org.sonar.wsclient.connectors.Connector; import org.sonar.wsclient.connectors.ConnectorFactory; import org.sonar.wsclient.services.*; +import org.sonar.wsclient.unmarshallers.UnmarshalException; import org.sonar.wsclient.unmarshallers.Unmarshaller; import org.sonar.wsclient.unmarshallers.Unmarshallers; @@ -48,8 +49,12 @@ public class Sonar { String json = connector.execute(query); MODEL result = null; if (json != null) { - Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass()); - result = unmarshaller.toModel(json); + try { + Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass()); + result = unmarshaller.toModel(json); + } catch (Exception e) { + throw new UnmarshalException(query, json, e); + } } return result; } @@ -60,8 +65,12 @@ public class Sonar { if (json == null) { result = Collections.emptyList(); } else { - Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass()); - result = unmarshaller.toModels(json); + try { + Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass()); + result = unmarshaller.toModels(json); + } catch (Exception e) { + throw new UnmarshalException(query, json, e); + } } return result; } @@ -70,8 +79,12 @@ public class Sonar { String json = connector.execute(query); MODEL result = null; if (json != null) { - Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass()); - result = unmarshaller.toModel(json); + try { + Unmarshaller<MODEL> unmarshaller = Unmarshallers.forModel(query.getModelClass()); + result = unmarshaller.toModel(json); + } catch (Exception e) { + throw new UnmarshalException(query, json, e); + } } return result; } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/UnmarshalException.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/UnmarshalException.java new file mode 100644 index 00000000000..860356158b0 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/UnmarshalException.java @@ -0,0 +1,36 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.unmarshallers; + +import org.sonar.wsclient.services.AbstractQuery; +import org.sonar.wsclient.services.Query; + +/** + * @since 2.10 + */ +public final class UnmarshalException extends RuntimeException { + public UnmarshalException(String s) { + super(s); + } + + public UnmarshalException(AbstractQuery query, String json, Throwable t) { + super("Can not parse the response of query " + query.getUrl() + ": " + json, t); + } +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/BadRulesServlet.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/BadRulesServlet.java new file mode 100644 index 00000000000..3b01f0abe82 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/BadRulesServlet.java @@ -0,0 +1,38 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient; + +import javax.servlet.GenericServlet; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +public class BadRulesServlet extends GenericServlet { + + static final String JSON = "[{'foo': 'bar'}]"; + + @Override + public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { + PrintWriter out = response.getWriter(); + out.println(JSON); + } +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarTest.java index 716919076ea..13f5b8c0152 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarTest.java @@ -19,14 +19,6 @@ */ package org.sonar.wsclient; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.number.OrderingComparisons.greaterThan; -import static org.junit.Assert.assertThat; - -import java.util.Arrays; -import java.util.Collection; - import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,11 +27,18 @@ import org.mortbay.jetty.testing.ServletTester; import org.sonar.wsclient.connectors.ConnectionException; import org.sonar.wsclient.connectors.HttpClient3Connector; import org.sonar.wsclient.connectors.HttpClient4Connector; -import org.sonar.wsclient.services.Metric; -import org.sonar.wsclient.services.MetricQuery; -import org.sonar.wsclient.services.Query; -import org.sonar.wsclient.services.Server; -import org.sonar.wsclient.services.ServerQuery; +import org.sonar.wsclient.services.*; +import org.sonar.wsclient.unmarshallers.UnmarshalException; + +import java.util.Arrays; +import java.util.Collection; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.number.OrderingComparisons.greaterThan; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.internal.matchers.StringContains.containsString; @RunWith(value = Parameterized.class) public class SonarTest { @@ -65,12 +64,13 @@ public class SonarTest { tester.addServlet(ServerServlet.class, "/api/server/index"); tester.addServlet(MetricServlet.class, "/api/metrics"); tester.addServlet(EmptyServlet.class, "/api/empty"); + tester.addServlet(BadRulesServlet.class, "/api/rules"); baseUrl = tester.createSocketConnector(true); tester.start(); - return Arrays.asList(new Object[][] { - { new Sonar(new HttpClient4Connector(new Host(baseUrl))) }, - { new Sonar(new HttpClient3Connector(new Host(baseUrl))) } + return Arrays.asList(new Object[][]{ + {new Sonar(new HttpClient4Connector(new Host(baseUrl)))}, + {new Sonar(new HttpClient3Connector(new Host(baseUrl)))} }); } @@ -111,6 +111,17 @@ public class SonarTest { assertThat(server.getVersion(), is("2.0")); } + @Test + public void shouldPropagateUnmarshalContext() { + try { + sonar.findAll(new RuleQuery("java")); + fail(); + } catch (UnmarshalException ue) { + assertThat(ue.getMessage(), containsString("/api/rules")); // contains request url + assertThat(ue.getMessage(), containsString(BadRulesServlet.JSON)); // contains response + } + } + static class EmptyQuery extends Query<Metric> { public String getUrl() { return "/api/empty"; |