Skip to content

Commit

Permalink
Merge pull request #54 from badoo/improve_exception_handling
Browse files Browse the repository at this point in the history
Improve exception handling
  • Loading branch information
paulmihaicraciunas authored Dec 18, 2023
2 parents 4894118 + 45e6925 commit e07de46
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
11 changes: 9 additions & 2 deletions core/src/main/kotlin/com/malinskiy/marathon/Marathon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.malinskiy.marathon.cache.test.TestCacheSaver
import com.malinskiy.marathon.config.LogicalConfigurationValidator
import com.malinskiy.marathon.device.DeviceProvider
import com.malinskiy.marathon.exceptions.NoDevicesException
import com.malinskiy.marathon.exceptions.ReportGenerationException
import com.malinskiy.marathon.execution.ComponentInfo
import com.malinskiy.marathon.execution.ComponentInfoExtractor
import com.malinskiy.marathon.execution.Configuration
Expand Down Expand Up @@ -175,13 +176,15 @@ class Marathon(
scheduler.stopAndWaitForCompletion()
onFinish(analytics, deviceProvider, attachmentManager)
} catch (throwable: Throwable) {
// We don't want to catch these. If an exception was thrown, we should fail the execution
log.error("Error occurred while finishing tests run", throwable)
throw throwable
} finally {
hook.uninstall()

stopKoin()
return progressReporter.aggregateResult()
}
return progressReporter.aggregateResult()
}

private fun installShutdownHook(block: suspend () -> Unit): ShutdownHook {
Expand All @@ -202,7 +205,11 @@ class Marathon(
analytics.close()
deviceProvider.terminate()
attachmentManager.terminate()
tracker.close()
try {
tracker.close()
} catch (e: Throwable) {
throw ReportGenerationException("Failed to generate test run report with exception", e)
}
}

private fun applyTestFilters(parsedTests: List<Test>): List<Test> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.malinskiy.marathon.exceptions

class FailedToPullScreenshotsException(message: String, cause: Throwable) : RuntimeException(message, cause)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.malinskiy.marathon.exceptions

class ReportGenerationException(message: String, cause: Throwable) : RuntimeException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.malinskiy.marathon.android.AndroidDevice
import com.malinskiy.marathon.android.executor.listeners.TestRunListener
import com.malinskiy.marathon.device.DevicePoolId
import com.malinskiy.marathon.device.toDeviceInfo
import com.malinskiy.marathon.exceptions.FailedToPullScreenshotsException
import com.malinskiy.marathon.execution.FilteringConfiguration
import com.malinskiy.marathon.execution.matches
import com.malinskiy.marathon.log.MarathonLogging
Expand Down Expand Up @@ -75,7 +76,8 @@ class PullScreenshotTestRunListener(
}
logger.trace { "Pulling screenshots finished in ${millis}ms from $remoteDir to $outputDir" }
} catch (e: Exception) {
logger.error(e) { "Failed to pull screenshots from $remoteDir" }
// It's unlikely we can continue from this point if pulling screenshots fails
throw FailedToPullScreenshotsException("Failed to pull screenshots from $remoteDir", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AndroidDeviceTestRunner(private val device: DdmlibAndroidDevice) {
} finally {

}
// Do not catch FailedToPullScreenshotsException. If that's thrown, we should fail the whole task
}

private fun notifyIgnoredTest(ignoredTests: List<Test>, listeners: ITestRunListener) {
Expand Down

0 comments on commit e07de46

Please sign in to comment.