Skip to content

Commit

Permalink
Report "invalid state" message will group correctly in sentry; detail…
Browse files Browse the repository at this point in the history
…ed cause moved to cause (#861)
  • Loading branch information
dsvoronin authored Mar 22, 2021
1 parent 9f51638 commit fa1d9a4
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ class ReportImplementation(
exception: Throwable,
screenshot: FutureValue<RemoteStorage.Result>?,
) = methodExecutionTracing("registerIncident") {
val currentState = getCastedState<ReportState.Initialized> {
IllegalStateException(it, exception)
}
val currentState = getCastedState<ReportState.Initialized>(cause = exception)

if (currentState.incident == null) {
if (screenshot != null) {
Expand Down Expand Up @@ -335,11 +333,29 @@ class ReportImplementation(
}
}

private inline fun <reified T : ReportState> getCastedState(
exceptionBuilder: (String) -> IllegalStateException = { IllegalStateException(it) }
): T {
return getCastedStateOrNull()
?: throw exceptionBuilder("Invalid state. Expected ${T::class.java} actual $state")
private inline fun <reified T : ReportState> getCastedState(cause: Throwable? = null): T {
val castedClass = getCastedStateOrNull<T>()

if (castedClass == null) {
val _state = state

val detailedCause = cause ?: when (_state) {
is ReportState.Initialized.NotStarted ->
IllegalStateException("Test not started, incident = ${_state.incident}")

is ReportState.Initialized.Started -> null
ReportState.Nothing -> null
ReportState.Written -> null
}

throw IllegalStateException(
"Invalid state. " +
"Expected ${T::class.simpleName} actual ${state::class.java.simpleName}",
detailedCause
)
} else {
return castedClass
}
}

private fun StepResult.appendFutureEntries(): StepResult {
Expand Down

0 comments on commit fa1d9a4

Please sign in to comment.