Skip to content

Commit

Permalink
better logging + try catch around execution
Browse files Browse the repository at this point in the history
  • Loading branch information
wandmagic committed Nov 7, 2024
1 parent 3f26df0 commit ec8b28b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlinx.coroutines.async
import java.util.UUID
import io.vertx.ext.web.handler.StaticHandler
import io.vertx.ext.web.handler.BodyHandler
import gov.nist.secauto.metaschema.cli.processor.ExitCode
import gov.nist.secauto.metaschema.cli.processor.ExitCode.RUNTIME_ERROR
import gov.nist.secauto.metaschema.cli.processor.ExitStatus
import gov.nist.secauto.metaschema.cli.processor.MessageExitStatus
import io.vertx.ext.web.Router
Expand Down Expand Up @@ -136,10 +138,7 @@ class OscalVerticle : CoroutineVerticle() {


private fun processUrl(url: String): String {
logger.info("processUrl input: $url")

if (!url.startsWith("file://")) {
logger.info("processUrl output (unchanged): $url")
return url
}

Expand All @@ -162,13 +161,9 @@ class OscalVerticle : CoroutineVerticle() {
// Unix-like systems
decodedPath
}
}

logger.info("processUrl output: $result")
}
return result
} catch (e: Exception) {
logger.error("Error processing file URL: $url", e)
logger.info("processUrl output (error case): $url")
return url
}
}
Expand Down Expand Up @@ -419,7 +414,12 @@ class OscalVerticle : CoroutineVerticle() {
mutableArgs.add(sarifFilePath)

logger.info(mutableArgs.joinToString(" "))
val exitStatus = CLI.runCli(*mutableArgs.toTypedArray())

val exitStatus = try {
CLI.runCli(*mutableArgs.toTypedArray())
} catch (e: Exception) {
MessageExitStatus(ExitCode.RUNTIME_ERROR, e.message)
}
// Check if SARIF file was created
if (!File(sarifFilePath).exists()) {
val exitCode = "code:${exitStatus.exitCode}"
Expand All @@ -445,7 +445,7 @@ class OscalVerticle : CoroutineVerticle() {
}

private fun createBasicSarif(errorMessage: String): String {
val version = OscalCliVersion();
val version = OscalCliVersion().getVersion();
return """
{
"${'$'}schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
Expand Down Expand Up @@ -474,7 +474,6 @@ class OscalVerticle : CoroutineVerticle() {
}
private fun sendSuccessResponse(ctx: RoutingContext, exitStatus: ExitStatus, sarifFilePath: String) {
val fileContent = File(sarifFilePath).readText()
File(sarifFilePath).delete()
ctx.response()
.setStatusCode(200) // HTTP 200 OK
.putHeader("Exit-Status", exitStatus.exitCode.toString())
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PatternLayout pattern="${LOG_PATTERN}" disableAnsi="false"/>
<Filters>
<!-- Allow INFO level and above -->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</Console>
</Appenders>
Expand Down

0 comments on commit ec8b28b

Please sign in to comment.