Skip to content

Commit

Permalink
Merge pull request #1804 from robstoll/workaround/double-printing-err…
Browse files Browse the repository at this point in the history
…or-js

workaround for the double exception msg printing problem in JS
  • Loading branch information
robstoll authored Aug 2, 2024
2 parents 641205d + ea78eb7 commit c6ca149
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ expect class AtriumError internal constructor(message: String) : AssertionError
}
}

internal fun createAtriumError(message: String, errorAdjuster: AtriumErrorAdjuster): AtriumError {
val err = AtriumError(message)
errorAdjuster.adjust(err)
return err
}
internal fun createAtriumError(message: String, errorAdjuster: AtriumErrorAdjuster): AtriumError =
AtriumError(message).also {
errorAdjuster.adjust(it)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.tutteli.atrium.reporting

import ch.tutteli.atrium.core.polyfills.stackBacktrace

/**
* Indicates that an assertion made by Atrium failed.
*
Expand All @@ -11,7 +13,34 @@ package ch.tutteli.atrium.reporting
*
* To create such an error you need to use the [AtriumError.Companion.create][Companion.create] function.
*/
actual class AtriumError internal actual constructor(message: String) : AssertionError(message) {
actual class AtriumError private constructor(
private val internalMessage: String,
@Suppress("UNUSED_PARAMETER") dummyUnit: Unit
) : AssertionError(internalMessage) {
internal actual constructor(message: String) : this(message, Unit)

override val message: String?
get() {
val process = js("process.env._") as? String
return if (process?.contains("idea") == true) {
"".also {
// if the process contains idea, then we assume we deal with intellij-idea and that it suffers
// from the double print exception message problem by outputting the message once as message and
// once as part of the stack-trace (no idea why they do it).
// Currently, kotlin calls `message` twice, once when populating the stacktrace and once when
// reporting the failure. To work around the bug we return an empty string for the `message`
// and instead print it when intellij most likely wants to report the failure
//
// we tried to sneak it into the stack (as it is a String) but it looks like intellij-idea does a
// post-processing of the stack and double prints it again *sight*
if (Error().stackBacktrace.first().contains("AtriumError")) {
println("\n" + internalMessage)
}
}
} else {
internalMessage
}
}

actual companion object {
/**
Expand Down

0 comments on commit c6ca149

Please sign in to comment.