-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#52 Return failureCause for runs via the API
Still need to - persist it to the DB for later retrieval after-the-fact - possibly change the status code to a `4xx` error on the synchronous trigger API when the run is failed
- Loading branch information
1 parent
8d1eb8b
commit fe99bf9
Showing
4 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package recce.server.util | ||
|
||
import com.google.common.base.Throwables | ||
|
||
object ThrowableUtils { | ||
fun extractFailureCause(throwable: Throwable): String { | ||
val rootCause = Throwables.getRootCause(throwable) | ||
return if (rootCause.equals(throwable)) { | ||
throwable.messageOrClassName() | ||
} else { | ||
"${throwable.messageOrClassName()}, rootCause=[${rootCause.messageOrClassName()}]" | ||
} | ||
} | ||
|
||
private fun Throwable.messageOrClassName(): String = this.message ?: this.javaClass.simpleName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package recce.server.util | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class ThrowableUtilsTest { | ||
|
||
private val rootCause = IllegalArgumentException("root") | ||
private val causedByRoot = IllegalArgumentException("causedByRoot", rootCause) | ||
private val causedByCausedBy = IllegalArgumentException( | ||
"causedByCausedByRoot" + | ||
"", | ||
causedByRoot | ||
) | ||
|
||
@Test | ||
fun `should return just message for throwable with no cause`() { | ||
assertThat(ThrowableUtils.extractFailureCause(rootCause)) | ||
.isEqualTo("root") | ||
} | ||
|
||
@Test | ||
fun `should return message with root cause for throwable with cause`() { | ||
assertThat(ThrowableUtils.extractFailureCause(causedByRoot)) | ||
.isEqualTo("causedByRoot, rootCause=[root]") | ||
} | ||
|
||
@Test | ||
fun `should ignore intermediary exceptions`() { | ||
assertThat(ThrowableUtils.extractFailureCause(causedByCausedBy)) | ||
.isEqualTo("causedByCausedByRoot, rootCause=[root]") | ||
} | ||
|
||
@Test | ||
fun `should return exception type if there is no message`() { | ||
val noMessageRoot = IllegalArgumentException() | ||
assertThat(ThrowableUtils.extractFailureCause(noMessageRoot)) | ||
.isEqualTo("IllegalArgumentException") | ||
} | ||
|
||
@Test | ||
fun `should return exception type if there is no message for throwable with cause`() { | ||
val noMessageRoot = IllegalArgumentException() | ||
val bad = IllegalCallerException("bad", noMessageRoot) | ||
assertThat(ThrowableUtils.extractFailureCause(bad)) | ||
.isEqualTo("bad, rootCause=[IllegalArgumentException]") | ||
} | ||
} |