OSDN Git Service

feat: add calc exit code method
authorhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Wed, 9 Mar 2022 10:13:59 +0000 (19:13 +0900)
committerhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Wed, 9 Mar 2022 10:13:59 +0000 (19:13 +0900)
Crash-Report/2022-03-09-18-22-28.txt [new file with mode: 0644]
src/com/ranfa/lib/handler/CrashHandler.java

diff --git a/Crash-Report/2022-03-09-18-22-28.txt b/Crash-Report/2022-03-09-18-22-28.txt
new file mode 100644 (file)
index 0000000..3e06c29
--- /dev/null
@@ -0,0 +1,53 @@
+---- DelesteRandomSelector Crash Report ----
+// I may be so bad to code?
+
+
+Time: 22/03/09 18:22
+Description: Unexpected Error
+
+
+These are simplified stack trace. (shown up to 5 lines. Full Stack trace is below.)
+java.lang.IllegalStateException: java.util.concurrent.CompletionException: java.lang.IllegalArgumentException: Level must not negative.
+       at com.ranfa.main.DelesteRandomSelector.lambda$20(DelesteRandomSelector.java:412)
+       at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:859)
+       at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:837)
+       at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:478)
+       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
+
+
+A detailed walkthrough of the error, its code path and all known details is as follows:
+---------------------------------------------------------------------------------------
+
+
+Stacktrace:
+       at com.ranfa.main.DelesteRandomSelector.lambda$20(DelesteRandomSelector.java:412)
+       at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:859)
+       at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:837)
+       at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:478)
+       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
+       at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
+       at java.base/java.lang.Thread.run(Thread.java:829)
+Caused by: java.util.concurrent.CompletionException: java.lang.IllegalArgumentException: Level must not negative.
+       at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:314)
+       at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:319)
+       at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1739)
+       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
+       at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
+       at java.base/java.lang.Thread.run(Thread.java:829)
+Caused by: java.lang.IllegalArgumentException: Level must not negative.
+       at com.ranfa.lib.database.Scraping.getSpecificLevelSongs(Scraping.java:176)
+       at com.ranfa.main.DelesteRandomSelector.lambda$19(DelesteRandomSelector.java:399)
+       at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1736)
+       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
+       at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
+       at java.base/java.lang.Thread.run(Thread.java:829)
+
+
+-- System Details --
+Details:
+       DelesteRandomSelector Version: v3.1.0
+       Operating System: Windows 10 (amd64) version 10.0
+       Java Version: 11, Eclipse Adoptium
+       Java VM Version: OpenJDK 64-Bit Server VM, version 11.0.13+8
+       Memory: 84176784 bytes / 268435456 bytes up to 4273995776 bytes
+       JVM Flags: 1 total: [-Dfile.encoding=UTF-8]
index db2f096..0b654dc 100644 (file)
@@ -8,6 +8,7 @@ import java.lang.management.RuntimeMXBean;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Random;
+import java.util.concurrent.CompletionException;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -40,6 +41,12 @@ public class CrashHandler {
                                "// These Easter sentences were inspired by Minecraft Crash Report!"
                };
                
+               private static final int EXIT_WITH_NESTED_CAUSE = 2;
+               private static final int EXIT_WITH_NULL_POINTER_EXCEPTION = 3;
+               private static final int EXIT_WITH_ILLEGAL_STATE_EXCEPTION = 5;
+               private static final int EXIT_WITH_COMPLETION_EXCEPTION = 7;
+               private static final int EXIT_WITH_ILLEGAL_ARGUMENT_EXCEPTION = 9;
+               
                public CrashHandler() {
                        this(DEFAULT_DESCRIPTION);
                }
@@ -66,7 +73,9 @@ public class CrashHandler {
                public void execute() {
                        if(e == null)
                                throw new NullPointerException("Cannot execute crash because throwable is null.");
+                       estimateExitCode = calcExitCode();
                        LOG.error("Cannot keep up application! : {}", e.toString());
+                       LOG.error("Application will exit with exit code : {}", estimateExitCode);
                        LOG.error(outputReport());
                        crashReportLines.outCrashReport();
                        System.exit(estimateExitCode);
@@ -148,4 +157,37 @@ public class CrashHandler {
                public int getEstimateExitCode() {
                        return estimateExitCode;
                }
+               
+               private int calcExitCode() {
+                       int res = 1;
+                       if(e.getCause() != null)
+                               res *= EXIT_WITH_NESTED_CAUSE;
+                       if(e instanceof NullPointerException )
+                               res *= EXIT_WITH_NULL_POINTER_EXCEPTION;
+                       if(e instanceof IllegalStateException)
+                               res *= EXIT_WITH_ILLEGAL_STATE_EXCEPTION;
+                       if(e instanceof CompletionException     )
+                               res *= EXIT_WITH_COMPLETION_EXCEPTION;
+                       if(e instanceof IllegalArgumentException)
+                               res *= EXIT_WITH_ILLEGAL_ARGUMENT_EXCEPTION;
+                       if(e.getCause() != null)
+                               return calcExitCode(e.getCause(), res);
+                       return res;
+               }
+               
+               private static int calcExitCode(Throwable e, int current) {
+                       if(e == null)
+                               return current;
+                       if(e.getCause() != null)
+                               current *= EXIT_WITH_NESTED_CAUSE;
+                       if(e instanceof NullPointerException )
+                               current *= EXIT_WITH_NULL_POINTER_EXCEPTION;
+                       if(e instanceof IllegalStateException)
+                               current *= EXIT_WITH_ILLEGAL_STATE_EXCEPTION;
+                       if(e instanceof CompletionException     )
+                               current *= EXIT_WITH_COMPLETION_EXCEPTION;
+                       if(e instanceof IllegalArgumentException)
+                               current *= EXIT_WITH_ILLEGAL_ARGUMENT_EXCEPTION;
+                       return calcExitCode(e.getCause(), current);
+               }
 }