incubator-isis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From danhayw...@apache.org
Subject svn commit: r1185785 - in /incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer: JsonApplication.java RuntimeExceptionMapper.java
Date Tue, 18 Oct 2011 18:38:26 GMT
Author: danhaywood
Date: Tue Oct 18 18:38:26 2011
New Revision: 1185785

URL: http://svn.apache.org/viewvc?rev=1185785&view=rev
Log:
ISIS-109: added an exception mapper for arbitrary (unexpected) runtime exceptions

Added:
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/RuntimeExceptionMapper.java
Modified:
    incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/JsonApplication.java

Modified: incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/JsonApplication.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/JsonApplication.java?rev=1185785&r1=1185784&r2=1185785&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/JsonApplication.java
(original)
+++ incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/JsonApplication.java
Tue Oct 18 18:38:26 2011
@@ -36,6 +36,7 @@ public class JsonApplication extends Abs
         addClass(CapabilitiesResourceServerside.class);
         
         addSingleton(new JsonApplicationExceptionMapper());
+        addSingleton(new RuntimeExceptionMapper());
         
         // TODO: doesn't get injected
         // addSingleton(new TypedReprBuilderFactoryRegistry());

Added: incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/RuntimeExceptionMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/RuntimeExceptionMapper.java?rev=1185785&view=auto
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/RuntimeExceptionMapper.java
(added)
+++ incubator/isis/trunk/framework/viewer/json/json-viewer/src/main/java/org/apache/isis/viewer/json/viewer/RuntimeExceptionMapper.java
Tue Oct 18 18:38:26 2011
@@ -0,0 +1,80 @@
+package org.apache.isis.viewer.json.viewer;
+
+import java.util.List;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import org.apache.commons.lang.exception.ExceptionUtils;
+import org.apache.isis.viewer.json.applib.RestfulMediaType;
+import org.apache.isis.viewer.json.applib.RestfulResponse.HttpStatusCode;
+import org.apache.isis.viewer.json.applib.util.JsonMapper;
+
+import com.google.common.collect.Lists;
+
+@Provider
+public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> {
+
+    @Override
+    public Response toResponse(RuntimeException ex) {
+        ResponseBuilder builder = 
+                Response.status(HttpStatusCode.INTERNAL_SERVER_ERROR.getJaxrsStatusType())
+                .type(RestfulMediaType.APPLICATION_JSON_ERROR)
+                .entity(jsonFor(ex));
+        return builder.build();
+    }
+
+
+    private static class ExceptionPojo {
+
+        public static ExceptionPojo create(Exception ex) {
+            return new ExceptionPojo(ex);
+        }
+
+        private static String format(StackTraceElement stackTraceElement) {
+            return stackTraceElement.toString();
+        }
+
+        private final String message;
+        private final List<String> stackTrace = Lists.newArrayList();
+        private ExceptionPojo causedBy;
+
+        public ExceptionPojo(Throwable ex) {
+            this.message = ex.getMessage();
+            StackTraceElement[] stackTraceElements = ex.getStackTrace();
+            for (StackTraceElement stackTraceElement : stackTraceElements) {
+                this.stackTrace.add(format(stackTraceElement));
+            }
+            Throwable cause = ex.getCause();
+            if(cause != null && cause != ex) {
+                this.causedBy = new ExceptionPojo(cause);
+            }
+        }
+
+        @SuppressWarnings("unused")
+        public String getMessage() {
+            return message;
+        }
+        
+        @SuppressWarnings("unused")
+        public List<String> getStackTrace() {
+            return stackTrace;
+        }
+        
+        @SuppressWarnings("unused")
+        public ExceptionPojo getCausedBy() {
+            return causedBy;
+        }
+    }
+    
+    static String jsonFor(Exception ex) {
+        try {
+            return JsonMapper.instance().write(ExceptionPojo.create(ex));
+        } catch (Exception e) {
+            // fallback
+            return "{ \"exception\": \"" + ExceptionUtils.getFullStackTrace(ex) + "\" }";
+        }
+    }
+}



Mime
View raw message