You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2020/01/16 18:28:06 UTC

[juneau] branch master updated: Refactor BasicRestCallHandler.convertThrowable().

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 5a82f33  Refactor BasicRestCallHandler.convertThrowable().
5a82f33 is described below

commit 5a82f33a9739f6465fd3924839666d8b016d4c05
Author: JamesBognar <ja...@apache.org>
AuthorDate: Thu Jan 16 13:27:34 2020 -0500

    Refactor BasicRestCallHandler.convertThrowable().
---
 .../apache/juneau/rest/exceptions/BasicTest.java   | 20 +++++++++++++++++++
 .../apache/juneau/rest/BasicRestCallHandler.java   | 23 ++++++++++++++++++++--
 .../org/apache/juneau/rest/RestMethodContext.java  |  5 +----
 3 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/juneau-rest/juneau-rest-server-utest/src/test/java/org/apache/juneau/rest/exceptions/BasicTest.java b/juneau-rest/juneau-rest-server-utest/src/test/java/org/apache/juneau/rest/exceptions/BasicTest.java
index 17c2804..d5a8a53 100644
--- a/juneau-rest/juneau-rest-server-utest/src/test/java/org/apache/juneau/rest/exceptions/BasicTest.java
+++ b/juneau-rest/juneau-rest-server-utest/src/test/java/org/apache/juneau/rest/exceptions/BasicTest.java
@@ -17,6 +17,7 @@ import static org.apache.juneau.rest.testutils.TestUtils.*;
 
 import org.apache.juneau.dto.swagger.*;
 import org.apache.juneau.json.*;
+import org.apache.juneau.parser.*;
 import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.rest.helper.*;
 import org.apache.juneau.rest.mock2.*;
@@ -975,4 +976,23 @@ public class BasicTest {
 	public void g01_thrownObjectDoesntMatchReturnType() throws Exception {
 		g.get("/thrownObjectDoesntMatchReturnType").execute().assertStatus(404);
 	}
+
+	//-----------------------------------------------------------------------------------------------------------------
+	// ParseException should produce BadRequest
+	//-----------------------------------------------------------------------------------------------------------------
+
+	@Rest
+	public static class H {
+		@RestMethod
+		public void getFoo() throws Exception {
+			throw new ParseException("Test");
+		}
+	}
+
+	static MockRest h = MockRest.create(H.class).build();
+
+	@Test
+	public void h01_parseExceptionCausesBadRequest() throws Exception {
+		h.get("/foo").execute().assertStatus(400);
+	}
 }
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestCallHandler.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestCallHandler.java
index 13ca6e0..78d952d 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestCallHandler.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestCallHandler.java
@@ -18,15 +18,18 @@ import static org.apache.juneau.internal.StringUtils.*;
 import static org.apache.juneau.rest.Enablement.*;
 
 import java.io.*;
+import java.lang.reflect.*;
 import java.util.*;
 
 import javax.servlet.*;
 import javax.servlet.http.*;
 
+import org.apache.juneau.*;
 import org.apache.juneau.http.StreamResource;
 import org.apache.juneau.http.annotation.*;
 import org.apache.juneau.rest.RestContext.*;
 import org.apache.juneau.http.exception.*;
+import org.apache.juneau.parser.*;
 import org.apache.juneau.reflect.*;
 import org.apache.juneau.rest.util.*;
 
@@ -263,16 +266,32 @@ public class BasicRestCallHandler implements RestCallHandler {
 	@SuppressWarnings("deprecation")
 	@Override
 	public Throwable convertThrowable(Throwable t) {
+
 		ClassInfo ci = ClassInfo.ofc(t);
-		if (ci.is(HttpRuntimeException.class))
+		if (ci.is(InvocationTargetException.class)) {
+			t = ((InvocationTargetException)t).getTargetException();
+			ci = ClassInfo.ofc(t);
+		}
+
+		if (ci.is(HttpRuntimeException.class)) {
 			t = ((HttpRuntimeException)t).getInner();
+			ci = ClassInfo.ofc(t);
+		}
+
 		if (ci.isChildOf(RestException.class) || ci.hasAnnotation(Response.class))
 			return t;
+
+		if (t instanceof ParseException || t instanceof InvalidDataConversionException)
+			return new BadRequest(t);
+
 		String n = t.getClass().getName();
-		if (n.contains("AccessDenied"))
+
+		if (n.contains("AccessDenied") || n.contains("Unauthorized"))
 			return new Unauthorized(t);
+
 		if (n.contains("Empty") || n.contains("NotFound"))
 			return new NotFound(t);
+
 		return t;
 	}
 
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
index 95c768f..5c8efcc 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
@@ -949,10 +949,7 @@ public class RestMethodContext extends BeanContext implements Comparable<RestMet
 				mi.toString(), mi.getFullName()
 			);
 		} catch (InvocationTargetException e) {
-			Throwable e2 = e.getTargetException();		// Get the throwable thrown from the doX() method.
-			if (e2 instanceof ParseException || e2 instanceof InvalidDataConversionException)
-				throw new BadRequest(e2);
-			throw toHttpException(e, InternalServerError.class);
+			throw e.getTargetException();
 		}
 		return SC_OK;
 	}