From 94371017ad0b04878d8cf4126486435cd0c5cdef Mon Sep 17 00:00:00 2001 From: Nikita Evdokimov Date: Fri, 23 Aug 2024 15:33:42 +0300 Subject: [PATCH] ISSUE-639: Attach both stack traces - original error's one and ours --- .../failure/FailureLoggingProviderImpl.kt | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/failure/FailureLoggingProviderImpl.kt b/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/failure/FailureLoggingProviderImpl.kt index c5f54ea21..cccce9578 100644 --- a/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/failure/FailureLoggingProviderImpl.kt +++ b/kaspresso/src/main/kotlin/com/kaspersky/kaspresso/failure/FailureLoggingProviderImpl.kt @@ -66,7 +66,7 @@ class FailureLoggingProviderImpl( error?.let { " because of ${error.javaClass.simpleName}" } ) - error?.let { throw it.describedWith(viewMatcher) } + error?.let { throw describeError(it, viewMatcher) } } /** @@ -76,28 +76,27 @@ class FailureLoggingProviderImpl( * * @return transformed [error]. */ - private fun Throwable.describedWith(viewMatcher: Matcher?): Throwable { - val newError = when { - this is PerformException -> { + private fun describeError(originalError: Throwable, viewMatcher: Matcher?): Throwable { + return when { + originalError is PerformException -> { PerformException.Builder() - .from(this) + .from(originalError) .apply { viewMatcher?.let { withViewDescription(it.toString()) } } .build() + .apply { addSuppressed(originalError) } } - this is AssertionError -> { - AssertionFailedError(message).initCause(this) + originalError is AssertionError -> { + AssertionFailedError(originalError.message) + .initCause(originalError) + .apply { addSuppressed(originalError) } } - isWebViewException(this) -> { + isWebViewException(originalError) -> { val message = StringBuilder("Failed to interact with web view! Usually it means that desired element is not found or JavaScript is disabled in web view") viewMatcher?.let { message.append("\nView description: ${it.describe()}") } - RuntimeException(message.toString()) + RuntimeException(message.toString()).apply { addSuppressed(originalError) } } - else -> this + else -> originalError.apply { addSuppressed(RuntimeException()) } } - newError.stackTrace = Thread.currentThread().stackTrace - newError.addSuppressed(this) - - return newError } private fun isWebViewException(throwable: Throwable): Boolean {