Skip to content

Commit

Permalink
add proper ignoreExitCode
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 12, 2024
1 parent 5be42ea commit 1575192
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/org/move/cli/runConfigurations/aptos/Aptos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,9 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
runner: CapturingProcessHandler.() -> ProcessOutput = { runProcessWithGlobalProgress() }
): AptosProcessResult<Unit> {
val processOutput = executeCommandLine(commandLine, colored, listener, runner)
.ignoreNonZeroExitCode()
.unwrapOrElse {
if (it !is RsProcessExecutionException.FailedWithNonZeroExitCode) {
return Err(it)
}
it.output
return Err(it)
}

val json = processOutput.stdout
Expand Down
31 changes: 20 additions & 11 deletions src/main/kotlin/org/move/openapiext/MvProcessResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@ import org.move.stdext.RsResult

typealias RsProcessResult<T> = RsResult<T, RsProcessExecutionException>

sealed class RsProcessExecutionOrDeserializationException : RuntimeException {
constructor(cause: Throwable) : super(cause)
constructor(message: String) : super(message)
sealed class RsProcessExecutionOrDeserializationException: RuntimeException {
constructor(cause: Throwable): super(cause)
constructor(message: String): super(message)
}

class RsDeserializationException(cause: JacksonException) :
class RsDeserializationException(cause: JacksonException):
RsProcessExecutionOrDeserializationException(cause)

sealed class RsProcessExecutionException : RsProcessExecutionOrDeserializationException {
constructor(message: String) : super(message)
constructor(cause: Throwable) : super(cause)
sealed class RsProcessExecutionException: RsProcessExecutionOrDeserializationException {
constructor(message: String): super(message)
constructor(cause: Throwable): super(cause)

abstract val commandLineString: String

class Start(
override val commandLineString: String,
cause: ExecutionException,
) : RsProcessExecutionException(cause)
): RsProcessExecutionException(cause)

class Canceled(
override val commandLineString: String,
val output: ProcessOutput,
message: String = errorMessage(commandLineString, output),
) : RsProcessExecutionException(message)
): RsProcessExecutionException(message)

class Timeout(
override val commandLineString: String,
val output: ProcessOutput,
) : RsProcessExecutionException(errorMessage(commandLineString, output))
): RsProcessExecutionException(errorMessage(commandLineString, output))

/** The process exited with non-zero exit code */
class FailedWithNonZeroExitCode(
override val commandLineString: String,
val output: ProcessOutput,
) : RsProcessExecutionException(errorMessage(commandLineString, output))
): RsProcessExecutionException(errorMessage(commandLineString, output))

companion object {
fun errorMessage(commandLineString: String, output: ProcessOutput): String = """
Expand All @@ -68,3 +68,12 @@ fun RsProcessResult<ProcessOutput>.ignoreExitCode(): RsResult<ProcessOutput, RsP
is RsProcessExecutionException.FailedWithNonZeroExitCode -> RsResult.Ok(err.output)
}
}

fun RsProcessResult<ProcessOutput>.ignoreNonZeroExitCode(): RsResult<ProcessOutput, RsProcessExecutionException> =
when (this) {
is RsResult.Ok -> this
is RsResult.Err -> when (err) {
is RsProcessExecutionException.FailedWithNonZeroExitCode -> RsResult.Ok(err.output)
else -> this
}
}

0 comments on commit 1575192

Please sign in to comment.