Skip to content

Commit

Permalink
[jssrc2cpg] - Graceful handling of error situation for jssrc2cpg (joe…
Browse files Browse the repository at this point in the history
…rnio#3911)

While processing `.ejs` files, if the astgen throws any error while
parsing the file. We encoutner this error in Finish method where we read
skiped file contents for the report. In case of `.ejs` files as we
preprocess and convert that into `.js` file. And for some reason this
`.js` file is not present at the expected place, we get file not found
exception. I have handled the situation gracefully in order to avoid
breaking the entire process of generating the CPG.
  • Loading branch information
pandurangpatil authored Dec 7, 2023
1 parent 185d465 commit 0db78f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class AstCreationPass(cpg: Cpg, astGenRunnerResult: AstGenRunnerResult, config:
astGenRunnerResult.skippedFiles.foreach { skippedFile =>
val (rootPath, fileName) = skippedFile
val filePath = Paths.get(rootPath, fileName)
val fileLOC = IOUtils.readLinesInFile(filePath).size
val fileLOC = Try(IOUtils.readLinesInFile(filePath)) match {
case Success(filecontent) => filecontent.size
case Failure(exception) =>
logger.warn(s"Failed to read file: '${filePath}'", exception)
0
}
report.addReportInfo(fileName, fileLOC)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ class EjsPassTest extends AbstractPassTest {
"user.name"
)
}

"invalid EJS file test" in AstFixture(
"""
|<body>
|<h1>Welcome <%@#$= user.name %></h1>
|</body>
|""".stripMargin,
"index.js.ejs"
) { cpg =>
cpg.file.l.size shouldBe 0
}
}

}

0 comments on commit 0db78f9

Please sign in to comment.