Skip to content

Commit

Permalink
Add exceptionOccurred method to InfosetOutputter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeckerle committed Nov 10, 2023
1 parent 8b9af1e commit 89da97d
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.apache.daffodil.runtime1.api.InfosetItem
import org.apache.daffodil.runtime1.api.InfosetSimpleElement
import org.apache.daffodil.runtime1.api.MetadataHandler
import org.apache.daffodil.runtime1.api.SimpleElementMetadata
import org.apache.daffodil.runtime1.infoset.ExceptionOccurredMixin
import org.apache.daffodil.runtime1.infoset.InfosetOutputter
import org.apache.daffodil.runtime1.infoset.InfosetOutputterImpl
import org.apache.daffodil.runtime1.processors.DataProcessor
Expand Down Expand Up @@ -72,7 +73,7 @@ class TestMetadataWalking {
override def endComplexElementMetadata(m: ComplexElementMetadata): Unit = buf += m
}

class GatherData extends InfosetOutputterImpl with InfosetOutputter {
class GatherData extends InfosetOutputterImpl with InfosetOutputter with ExceptionOccurredMixin {

private val buf = new ArrayBuffer[InfosetItem]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,5 @@ abstract class InfosetOutputterProxy extends InfosetOutputter {
override def endComplex(diComplex: InfosetComplexElement): Unit = infosetOutputter.endComplex(diComplex)
override def startArray(diArray: InfosetArray): Unit = infosetOutputter.startArray(diArray)
override def endArray(diArray: InfosetArray): Unit = infosetOutputter.endArray(diArray)
override def exceptionOccurred(e: Exception): Unit = infosetOutputter.exceptionOccurred(e)

Check warning on line 473 in daffodil-japi/src/main/scala/org/apache/daffodil/japi/infoset/Infoset.scala

View check run for this annotation

Codecov / codecov/patch

daffodil-japi/src/main/scala/org/apache/daffodil/japi/infoset/Infoset.scala#L473

Added line #L473 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ public void startArray(InfosetArray diArray) {
@Override
public void endArray(InfosetArray diArray) {
}

@Override
public void exceptionOccurred(Exception e) {
// do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ trait InfosetOutputter extends BlobMethodsMixin {
def endArray(diArray: InfosetArray): Unit

def getStatus(): Status

/**
* Override this to be fed any exceptions thrown by the start/end methods of this class.
* This is mostly to facilitate users debugging their code that uses the API.
*
* This saves the user of the API from having to wrap try/catch around their
* start/end method bodies.
*
* @param e the exception thrown by one of the other methods.
*/
def exceptionOccurred(e: Exception): Unit

}

/**
Expand Down Expand Up @@ -185,3 +197,10 @@ object Status extends Enumeration {
type Status = Value
val DONE, READY, VISITING = Value
}

trait ExceptionOccurredMixin {
def exceptionOccurred(e: Exception): Unit = {
val ex = e // because sometimes the debugger won't show the exception
ex.getCause() // good place for a breakpoint
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class InfosetWalker private (
outputterFunc
} catch {
case e: Exception => {
outputter.exceptionOccurred(e) // debugging hook for the user.
val cause = e.getCause
val msg = if (cause == null) e.toString else cause.toString
context.SDE("Failed to %s: %s", desc, msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import java.nio.charset.StandardCharsets

class JsonInfosetOutputter private (writer: java.io.Writer, pretty: Boolean)
extends InfosetOutputterImpl
with ExceptionOccurredMixin
with Indentable {

def this(os: java.io.OutputStream, pretty: Boolean) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import org.apache.daffodil.runtime1.api.InfosetSimpleElement
/**
* Ignores all infoset events, outputting nothing
*/
class NullInfosetOutputter() extends InfosetOutputterImpl {
class NullInfosetOutputter() extends InfosetOutputterImpl
with ExceptionOccurredMixin {

override def reset(): Unit = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import org.apache.daffodil.runtime1.api.InfosetSimpleElement
*
* InfosetOutputters to send all events
*/
class TeeInfosetOutputter(outputters: InfosetOutputter*) extends InfosetOutputterImpl {
class TeeInfosetOutputter(outputters: InfosetOutputter*) extends InfosetOutputterImpl
with ExceptionOccurredMixin {

override def reset(): Unit = {
outputters.foreach { _.reset() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.daffodil.lib.equality._
import org.apache.daffodil.lib.util.Maybe
import org.apache.daffodil.lib.xml.XMLUtils

trait XMLInfosetOutputterMixin {
trait XMLInfosetOutputterMixin extends ExceptionOccurredMixin {

def remapped(dataValueAsString: String) =
XMLUtils.remapXMLIllegalCharactersToPUA(dataValueAsString)
Expand Down Expand Up @@ -58,5 +58,4 @@ trait XMLInfosetOutputterMixin {
}
res
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,5 @@ abstract class InfosetInputterProxy extends InfosetInputter {
override def endComplex(diComplex: InfosetComplexElement): Unit = infosetOutputter.endComplex(diComplex)
override def startArray(diArray: InfosetArray): Unit = infosetOutputter.startArray(diArray)
override def endArray(diArray: InfosetArray): Unit = infosetOutputter.endArray(diArray)
override def exceptionOccurred(e: Exception): Unit = infosetOutputter.exceptionOccurred(e)

Check warning on line 462 in daffodil-sapi/src/main/scala/org/apache/daffodil/sapi/infoset/Infoset.scala

View check run for this annotation

Codecov / codecov/patch

daffodil-sapi/src/main/scala/org/apache/daffodil/sapi/infoset/Infoset.scala#L460-L462

Added lines #L460 - L462 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ case class TestInfosetOutputter() extends InfosetOutputter {
override def startArray(diArray: InfosetArray): Unit = {}

override def endArray(diArray: InfosetArray): Unit = {}

override def exceptionOccurred(e: Exception): Unit = {}
}

0 comments on commit 89da97d

Please sign in to comment.