Skip to content

Commit

Permalink
JS build: catch importYaml error, rethrow with YAML path
Browse files Browse the repository at this point in the history
  • Loading branch information
generalmimon committed Nov 4, 2023
1 parent e581718 commit 3af7a08
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.kaitai.struct.format

import io.kaitai.struct.JavaScriptImporter
import io.kaitai.struct.problems.ErrorInInput

import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import scala.scalajs.concurrent.JSExecutionContext

class JavaScriptClassSpecs(importer: JavaScriptImporter, firstSpec: ClassSpec)
Expand All @@ -12,13 +14,19 @@ class JavaScriptClassSpecs(importer: JavaScriptImporter, firstSpec: ClassSpec)
val MODE_ABS = "abs"

override def importRelative(name: String, path: List[String], inFile: Option[String]): Future[Option[ClassSpec]] =
doImport(name, MODE_REL)
doImport(name, path, MODE_REL)
override def importAbsolute(name: String, path: List[String], inFile: Option[String]): Future[Option[ClassSpec]] =
doImport(name, MODE_ABS)
doImport(name, path, MODE_ABS)

def doImport(name: String, mode: String): Future[Option[ClassSpec]] =
importer.importYaml(name, mode).toFuture.map { (yaml) =>
val yamlScala = JavaScriptKSYParser.yamlJavascriptToScala(yaml)
Some(ClassSpec.fromYaml(yamlScala, Some(name)))
}(JSExecutionContext.queue)
def doImport(name: String, path: List[String], mode: String): Future[Option[ClassSpec]] = {
implicit val ec: ExecutionContext = JSExecutionContext.queue

importer.importYaml(name, mode).toFuture
.transform((yaml) => {
val yamlScala = JavaScriptKSYParser.yamlJavascriptToScala(yaml)
Some(ClassSpec.fromYaml(yamlScala, Some(name)))
}, (err) => {
throw ErrorInInput(err, path).toException
})
}
}

0 comments on commit 3af7a08

Please sign in to comment.