diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-07-17 00:36:25 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-07-17 00:36:25 +0000 |
commit | d87b7a5e7430ab946c851208e45ddbf03330c98f (patch) | |
tree | a25d04d5dd5ba93de5b138164bb63a4528f07565 | |
parent | 3f56676b3bf9248726941f88018bf7b38db5e895 (diff) |
TUSCANY-1961 - Properly reporting business and runtime exceptions to clients using jsonRPC binding
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@677479 13f79535-47bb-0310-9956-ffa450edef68
10 files changed, 181 insertions, 51 deletions
diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java index da4461c1cd..c0d0415f91 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCServiceServlet.java @@ -42,6 +42,7 @@ import org.json.JSONObject; import org.osoa.sca.ServiceRuntimeException; import com.metaparadigm.jsonrpc.JSONRPCBridge; +import com.metaparadigm.jsonrpc.JSONRPCResult; import com.metaparadigm.jsonrpc.JSONRPCServlet; /** @@ -166,7 +167,6 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { response.setContentType("text/plain;charset=utf-8"); OutputStream out = response.getOutputStream(); byte[] bout = smd.getBytes("UTF-8"); - out.write(bout); out.flush(); out.close(); @@ -232,30 +232,27 @@ public class JSONRPCServiceServlet extends JSONRPCServlet { RuntimeWire wire = componentService.getRuntimeWire(binding, serviceContract); Operation jsonOperation = findOperation(method); Object result = null; - JSONObject jsonResponse = new JSONObject(); + try { - result = wire.invoke(jsonOperation, args); - try { + JSONObject jsonResponse = new JSONObject(); + result = wire.invoke(jsonOperation, args); + + try { jsonResponse.put("result", result); jsonResponse.putOpt("id", id); + //get response to send to client + return jsonResponse.toString().getBytes("UTF-8"); } catch (Exception e) { - throw new ServiceRuntimeException(e); + throw new ServiceRuntimeException("Unable to create JSON response", e); } } catch (InvocationTargetException e) { - try { - jsonResponse.put("error", e.getCause()); - jsonResponse.putOpt("id", id); - } catch (Exception e1) { - throw new ServiceRuntimeException(e); - } + JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, e.getCause() ); + return errorResult.toString().getBytes("UTF-8"); } catch(RuntimeException e) { - e.printStackTrace(); - throw e; + JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, e.getCause()); + return errorResult.toString().getBytes("UTF-8"); } - - //get response to send to client - return jsonResponse.toString().getBytes("UTF-8"); - } + } /** * Find the operation from the component service contract diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java new file mode 100644 index 0000000000..14ee22931d --- /dev/null +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/BusinessException.java @@ -0,0 +1,9 @@ +package org.apache.tuscany.sca.binding.jsonrpc; + +public class BusinessException extends Exception { + public BusinessException(String message) + { + super(message); + } + +} diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java index 584228abde..1aff8784f7 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/Echo.java @@ -26,4 +26,8 @@ package org.apache.tuscany.sca.binding.jsonrpc; public interface Echo { String echo(String msg); + + void echoRuntimeException() throws RuntimeException; + + void echoBusinessException() throws BusinessException; } diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java index e63cafd6a6..b62fab0840 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/EchoComponentImpl.java @@ -31,4 +31,14 @@ public class EchoComponentImpl implements Echo { System.out.println("Echo: "+ msg); return "echo: " + msg; } + + public void echoBusinessException() throws BusinessException { + throw new BusinessException("Business Exception"); + + } + + public void echoRuntimeException() throws RuntimeException { + throw new RuntimeException("Runtime Exception"); + + } } diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java index f937e80cf5..2ad3db1a55 100644 --- a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java +++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java @@ -59,9 +59,38 @@ public class JSONRPCServiceTestCase extends TestCase { WebResponse response = wc.getResource(request); assertEquals(200, response.getResponseCode()); + JSONObject jsonResp = new JSONObject(response.getText()); assertEquals("echo: Hello JSON-RPC", jsonResp.getString("result")); } + + public void testRuntimeException() throws Exception{ + JSONObject jsonRequest = new JSONObject("{ \"method\": \"echoRuntimeException\", \"params\": [], \"id\": 2}"); + + WebConversation wc = new WebConversation(); + WebRequest request = new PostMethodWebRequest( SERVICE_URL, new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")),"application/json"); + WebResponse response = wc.getResource(request); + + assertEquals(200, response.getResponseCode()); + + JSONObject jsonErr = new JSONObject(response.getText()).getJSONObject("error"); + + assertEquals("Runtime Exception", jsonErr.getString("msg")); + } + + public void testBusinessException() throws Exception{ + JSONObject jsonRequest = new JSONObject("{ \"method\": \"echoBusinessException\", \"params\": [], \"id\": 3}"); + + WebConversation wc = new WebConversation(); + WebRequest request = new PostMethodWebRequest( SERVICE_URL, new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")),"application/json"); + WebResponse response = wc.getResource(request); + + assertEquals(200, response.getResponseCode()); + + JSONObject jsonErr = new JSONObject(response.getText()).getJSONObject("error"); + + assertEquals("Business Exception", jsonErr.getString("msg")); + } } diff --git a/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html b/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html index c2277772bf..58f9f560a7 100644 --- a/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html +++ b/java/sca/modules/implementation-widget-runtime/src/test/resources/content/store.html @@ -34,7 +34,11 @@ //@Property var locale = Property("locale");
- function catalog_getResponse(items) {
+ function catalog_getResponse(items,exception) {
+ if(exception){
+ alert(exception.message);
+ return;
+ }
var catalog = "";
for (var i=0; i<items.length; i++)
catalog += '<input name="items" type="checkbox" value="' +
@@ -120,4 +124,4 @@ </form>
</div>
</body>
-</html>
\ No newline at end of file +</html> diff --git a/java/sca/samples/store/src/main/resources/uiservices/store.html b/java/sca/samples/store/src/main/resources/uiservices/store.html index 095570f949..45604463d1 100644 --- a/java/sca/samples/store/src/main/resources/uiservices/store.html +++ b/java/sca/samples/store/src/main/resources/uiservices/store.html @@ -35,20 +35,25 @@ var catalogItems; - function catalog_getResponse(items) { - var catalog = ""; - for (var i=0; i<items.length; i++) { - var item = items[i].name + ' - ' + items[i].price; - catalog += '<input name="items" type="checkbox" value="' + - item + '">' + item + ' <br>'; + function catalog_getResponse(items,exception) { + if(exception){ + alert(exception.message); + return; } - document.getElementById('catalog').innerHTML=catalog; - catalogItems = items; + var catalog = ""; + + for (var i=0; i<items.length; i++) { + var item = items[i].name + ' - ' + items[i].price; + catalog += '<input name="items" type="checkbox" value="' + + item + '">' + item + ' <br>'; + } + document.getElementById('catalog').innerHTML=catalog; + catalogItems = items; } function shoppingCart_getResponse(feed) { if (feed != null) { - var entries = feed.getElementsByTagName("entry"); + var entries = feed.getElementsByTagName("entry"); var list = ""; for (var i=0; i<entries.length; i++) { var content = entries[i].getElementsByTagName("content")[0]; @@ -59,12 +64,21 @@ document.getElementById("shoppingCart").innerHTML = list; if (entries.length != 0) { - shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + try { + shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + } + catch(e){ + alert(e); + } } } } - function shoppingTotal_getTotalResponse(total) { + function shoppingTotal_getTotalResponse(total,exception) { + if(exception) { + alert(exception.message); + return; + } document.getElementById('total').innerHTML = total; } @@ -77,6 +91,7 @@ var j = 0; for (var i=0; i<items.length; i++) if (items[i].checked) { + var entry = '<entry xmlns="http://www.w3.org/2005/Atom"><title>item</title><content type="text/xml">' + '<Item xmlns="http://services/">' + '<name xmlns="">' + catalogItems[i].name + '</name>' + '<price xmlns="">' + catalogItems[i].price + '</price>' + @@ -106,9 +121,15 @@ } function init() { - catalog.get(catalog_getResponse); - shoppingCart.get("", shoppingCart_getResponse); - } + + try { + catalog.get(catalog_getResponse); + shoppingCart.get("", shoppingCart_getResponse); + } + catch(e){ + alert(e); + } + } </script> diff --git a/java/sca/tutorial/assets/uiservices/store.html b/java/sca/tutorial/assets/uiservices/store.html index 095570f949..f2425b4885 100644 --- a/java/sca/tutorial/assets/uiservices/store.html +++ b/java/sca/tutorial/assets/uiservices/store.html @@ -35,7 +35,11 @@ var catalogItems; - function catalog_getResponse(items) { + function catalog_getResponse(items,exception) { + if(exception){ + alert(exception.message); + return; + } var catalog = ""; for (var i=0; i<items.length; i++) { var item = items[i].name + ' - ' + items[i].price; @@ -58,13 +62,22 @@ } document.getElementById("shoppingCart").innerHTML = list; - if (entries.length != 0) { - shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + if (entries.length != 0) { + try { + shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + } + catch(e) { + alert(e); + } } } } - function shoppingTotal_getTotalResponse(total) { + function shoppingTotal_getTotalResponse(total,exception) { + if(exception){ + alert(exception.message); + return; + } document.getElementById('total').innerHTML = total; } @@ -106,8 +119,13 @@ } function init() { - catalog.get(catalog_getResponse); - shoppingCart.get("", shoppingCart_getResponse); + try { + catalog.get(catalog_getResponse); + shoppingCart.get("", shoppingCart_getResponse); + } + catch(e) { + alert(e); + } } </script> diff --git a/java/sca/tutorial/store-eu/uiservices/store-eu.html b/java/sca/tutorial/store-eu/uiservices/store-eu.html index ec72d9b2a7..4569b76b3f 100644 --- a/java/sca/tutorial/store-eu/uiservices/store-eu.html +++ b/java/sca/tutorial/store-eu/uiservices/store-eu.html @@ -35,7 +35,11 @@ var catalogItems; - function catalog_getResponse(items) { + function catalog_getResponse(items, exception) { + if(exception){ + alert(exception.message); + return; + } var catalog = ""; for (var i=0; i<items.length; i++) { var item = items[i].name + ' - ' + items[i].price; @@ -58,13 +62,23 @@ } document.getElementById("shoppingCart").innerHTML = list; - if (entries.length != 0) { - shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + if (entries.length != 0) { + try { + shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + } + carch(e) { + alert(e); + } + } } } - function shoppingTotal_getTotalResponse(total) { + function shoppingTotal_getTotalResponse(total,exception) { + if(exception){ + alert(exception.message); + return; + } document.getElementById('total').innerHTML = total; } @@ -106,8 +120,14 @@ } function init() { - catalog.get(catalog_getResponse); - shoppingCart.get("", shoppingCart_getResponse); + try { + catalog.get(catalog_getResponse); + shoppingCart.get("", shoppingCart_getResponse); + } + catch(e) { + alert(e); + } + } </script> diff --git a/java/sca/tutorial/store-mashup/gadget/store-gadget.html b/java/sca/tutorial/store-mashup/gadget/store-gadget.html index c2a8ba5118..c09b9d28c8 100644 --- a/java/sca/tutorial/store-mashup/gadget/store-gadget.html +++ b/java/sca/tutorial/store-mashup/gadget/store-gadget.html @@ -35,7 +35,11 @@ var catalogItems; - function catalog_getResponse(items) { + function catalog_getResponse(items,exception) { + if(exception){ + alert(exception.message); + return; + } var catalog = ""; for (var i=0; i<items.length; i++) { var item = items[i].name + ' - ' + items[i].price; @@ -58,13 +62,22 @@ } document.getElementById("shoppingCart").innerHTML = list; - if (entries.length != 0) { - shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + if (entries.length != 0) { + try { + shoppingTotal.getTotal(shoppingTotal_getTotalResponse); + } + carch(e) { + alert(e); + } } } } - function shoppingTotal_getTotalResponse(total) { + function shoppingTotal_getTotalResponse(total,exception) { + if(exception){ + alert(exception.message); + return; + } document.getElementById('total').innerHTML = total; } @@ -113,8 +126,13 @@ } function init() { - catalog.get(catalog_getResponse); - shoppingCart.get("", shoppingCart_getResponse); + try { + catalog.get(catalog_getResponse); + shoppingCart.get("", shoppingCart_getResponse); + } + catch(e) { + alert(e); + } } </script> |