You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/01/22 14:48:42 UTC

[isis] 03/03: ISIS-2158: smarter exception to message utility

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

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

commit a58669bc9f06e5612d5f4bdb61fb99913daf554a
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Jan 22 15:48:30 2020 +0100

    ISIS-2158: smarter exception to message utility
---
 .../commons/internal/exceptions/_Exceptions.java   | 33 ++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/core/commons/src/main/java/org/apache/isis/core/commons/internal/exceptions/_Exceptions.java b/core/commons/src/main/java/org/apache/isis/core/commons/internal/exceptions/_Exceptions.java
index fd1f314..b22e1e3 100644
--- a/core/commons/src/main/java/org/apache/isis/core/commons/internal/exceptions/_Exceptions.java
+++ b/core/commons/src/main/java/org/apache/isis/core/commons/internal/exceptions/_Exceptions.java
@@ -31,6 +31,7 @@ import java.util.stream.Stream;
 
 import javax.annotation.Nullable;
 
+import org.apache.isis.core.commons.collections.Can;
 import org.apache.isis.core.commons.exceptions.IsisException;
 import org.apache.isis.core.commons.internal.base._NullSafe;
 import org.apache.isis.core.commons.internal.base._Strings;
@@ -132,6 +133,38 @@ public final class _Exceptions {
         return new UnsupportedOperationException("unrecoverable error: method call not allowed/supported");
     }
     
+    // -- MESSAGE
+    
+    public static String getMessage(Exception ex) {
+        if(ex==null) {
+            return "no exception present";
+        }
+        if(_Strings.isNotEmpty(ex.getMessage())) {
+            return ex.getMessage();
+        }
+        val sb = new StringBuilder();
+        val nestedMsg = streamCausalChain(ex)
+                .peek(throwable->{
+                    sb.append(throwable.getClass().getSimpleName()).append("/");
+                })
+                .map(Throwable::getMessage)
+                .filter(_NullSafe::isPresent)
+                .findFirst();
+        
+        if(nestedMsg.isPresent()) {
+            sb.append(nestedMsg.get());
+        } else {
+            
+            Can.ofArray(ex.getStackTrace())
+            .stream()
+            .limit(20)
+            .forEach(trace->sb.append("\n").append(trace));
+        }
+                
+        return sb.toString();
+    }
+    
+    // -- THROWING
     
     /**
      * Used to hide from the compiler the fact, that this call always throws.