Skip to content

Commit

Permalink
Add support for passing exception directly into DaffodilUnhandledSAXE…
Browse files Browse the repository at this point in the history
…xception

- add constructors for throwable only and message only
- add unit tests that create the DaffodilUnhandledSAXException and compares them to the message, the throwable or both

DAFFODIL-2433
  • Loading branch information
olabusayoT committed Mar 27, 2024
1 parent 3e936cf commit f2d0afb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import java.io.ByteArrayOutputStream
import org.apache.daffodil.lib.Implicits.intercept
import org.apache.daffodil.lib.xml.DaffodilSAXParserFactory
import org.apache.daffodil.lib.xml.XMLUtils
import org.apache.daffodil.runtime1.api.DFDL.DaffodilUnhandledSAXException

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.xml.sax.InputSource
Expand Down Expand Up @@ -200,4 +202,34 @@ class TestSAXUnparseAPI {
assertTrue(m.contains("prior to end"))
assertTrue(m.contains("{http://example.com}list"))
}

@Test def testDaffodilUnhandledSAXException_creation_bothMessageAndCause(): Unit = {
val message = "Error Message"
val thr = new IllegalArgumentException("Illegal Argument Message")
val e = new DaffodilUnhandledSAXException(message, thr)
assertEquals(message, e.getMessage)
// we call getCause twice here, because the cause is wrapped in an Exception before
// being returned
assertEquals(thr, e.getCause.getCause)
}

@Test def testDaffodilUnhandledSAXException_creation_onlyMessage(): Unit = {
val message = "Error Message"
val e = new DaffodilUnhandledSAXException(message)
assertEquals(message, e.getMessage)
// we call getCause twice here, because the cause is wrapped in an Exception before
// being returned
assertNull(e.getCause.getCause)
}

@Test def testDaffodilUnhandledSAXException_creation_onlyCause(): Unit = {
val thr = new IllegalArgumentException("Illegal Argument Message")
val e = new DaffodilUnhandledSAXException(thr)
// when the detailMessage is null as is the case when no message is passed in,
// getMessage returns the embedded exception as a string
assertEquals(thr.toString, e.getMessage)
// we call getCause twice here, because the cause is wrapped in an Exception before
// being returned
assertEquals(thr, e.getCause.getCause)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ object DFDL {
* Thrown by the DaffodilUnparseConentHandler when an unexpected error
* occurs, this usually represents a bug in Daffodil
*/
class DaffodilUnhandledSAXException(description: String, cause: Exception)
extends SAXException(description, cause)
class DaffodilUnhandledSAXException(description: String, cause: Throwable)
extends SAXException(description, new Exception(cause)) {
def this(msg: String) = this(msg, null)

def this(cause: Throwable) = this(null, cause)
}

trait ParseResult extends Result with WithDiagnostics {
def resultState: State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class DaffodilUnparseContentHandlerImpl(dp: DFDL.DataProcessor, output: DFDL.Out
case Left(e) => {
// unparse threw an unexpected exception, this is likely a bug. We don't
// have an UnparseResult so just rethrow the exception as a SAXException.
throw new DaffodilUnhandledSAXException(e.getMessage, e)
throw new DaffodilUnhandledSAXException(e)
}
// $COVERAGE-ON$
}
Expand Down

0 comments on commit f2d0afb

Please sign in to comment.