From 88bee8dfcdf72b711ee32f29576d4d2d981e142f Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Fri, 22 Mar 2024 18:44:20 +0100 Subject: [PATCH] Improved error message obtention Improved error message obtention by looking into causes instead of the top level exception --- .../exceptions/CommandExecutionFailed.kt | 4 ++++ .../gitnuro/exceptions/GitnuroException.kt | 2 +- .../com/jetpackduba/gitnuro/git/TabState.kt | 22 +++++++++++++++++-- .../kotlin/com/jetpackduba/gitnuro/main.kt | 1 + .../gitnuro/managers/ShellManager.kt | 8 ++++++- 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/com/jetpackduba/gitnuro/exceptions/CommandExecutionFailed.kt diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/CommandExecutionFailed.kt b/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/CommandExecutionFailed.kt new file mode 100644 index 0000000..a1f3d0d --- /dev/null +++ b/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/CommandExecutionFailed.kt @@ -0,0 +1,4 @@ +package com.jetpackduba.gitnuro.exceptions + +class CommandExecutionFailed(msg: String, cause: Exception) : GitnuroException(msg, cause) { +} \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/GitnuroException.kt b/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/GitnuroException.kt index 6037daf..ec3051d 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/GitnuroException.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/exceptions/GitnuroException.kt @@ -1,3 +1,3 @@ package com.jetpackduba.gitnuro.exceptions -abstract class GitnuroException(msg: String) : RuntimeException(msg) \ No newline at end of file +abstract class GitnuroException(msg: String, cause: Exception? = null) : Exception(msg, cause) \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt index 54bb08a..b3a7436 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/TabState.kt @@ -1,6 +1,7 @@ package com.jetpackduba.gitnuro.git import com.jetpackduba.gitnuro.di.TabScope +import com.jetpackduba.gitnuro.exceptions.GitnuroException import com.jetpackduba.gitnuro.extensions.delayedStateChange import com.jetpackduba.gitnuro.git.log.FindCommitUseCase import com.jetpackduba.gitnuro.logging.printError @@ -143,8 +144,11 @@ class TabState @Inject constructor( val containsCancellation = exceptionContainsCancellation(ex) - if (!containsCancellation) - errorsManager.addError(newErrorNow(ex, null, ex.message.orEmpty())) + if (!containsCancellation) { + val innerException = getInnerException(ex) + + errorsManager.addError(newErrorNow(innerException, null, innerException.message.orEmpty())) + } printError(TAG, ex.message.orEmpty(), ex) } finally { @@ -163,6 +167,20 @@ class TabState @Inject constructor( return job } + private fun getInnerException(ex: Exception): Exception { + return if (ex is GitnuroException) { + ex + } else { + val cause = ex.cause + + if (cause != null && cause is Exception) { + getInnerException(cause) + } else { + ex + } + } + } + private fun exceptionContainsCancellation(ex: Throwable?): Boolean { return when (ex) { null -> false diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/main.kt b/src/main/kotlin/com/jetpackduba/gitnuro/main.kt index f829778..a3e4f8a 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/main.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/main.kt @@ -1,5 +1,6 @@ package com.jetpackduba.gitnuro +import com.jetpackduba.gitnuro.managers.ShellManager import com.jetpackduba.gitnuro.preferences.initPreferencesPath fun main(args: Array) { diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/managers/ShellManager.kt b/src/main/kotlin/com/jetpackduba/gitnuro/managers/ShellManager.kt index 447cbef..1caf53a 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/managers/ShellManager.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/managers/ShellManager.kt @@ -1,5 +1,6 @@ package com.jetpackduba.gitnuro.managers +import com.jetpackduba.gitnuro.exceptions.CommandExecutionFailed import com.jetpackduba.gitnuro.logging.printError import com.jetpackduba.gitnuro.logging.printLog import java.io.File @@ -79,7 +80,12 @@ class ShellManager @Inject constructor() : IShellManager { override fun runCommandProcess(command: List): Process { printLog(TAG, "runCommandProcess: " + command.joinToString(" ")) - return ProcessBuilder(command).start() + try { + return ProcessBuilder(command).start() +// return ProcessBuilder(command).start() + } catch (ex: IOException) { + throw CommandExecutionFailed(ex.message.orEmpty(), ex) + } } }