Skip to content

Commit

Permalink
fixup! fixup! fixup! Add support for escalating SDWs to Errors
Browse files Browse the repository at this point in the history
- make RSDW and RSDE extend SDW and SDEs respectively
- setup SDEFromWarning to be caught for unparser errors
- fix bug with doSDE where we pass in null instead of the exception message

DAFFODIL-2810
  • Loading branch information
olabusayoT committed Oct 2, 2024
1 parent 316dba1 commit 175471a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ final class RuntimeExpressionDPath[T <: AnyRef](
if (value.asInstanceOf[String].length == 0) {
val e = new RuntimeSchemaDefinitionError(
ci.schemaFileLocation,
state,
"Non-empty string required."
)
doSDE(e, state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.apache.daffodil.lib.exceptions.SchemaFileLocation
import org.apache.daffodil.lib.exceptions._
import org.apache.daffodil.lib.util.Maybe
import org.apache.daffodil.lib.util.Maybe._
import org.apache.daffodil.runtime1.processors.CompileState
import org.apache.daffodil.runtime1.processors.ParseOrUnparseState

class SchemaDefinitionError(
Expand All @@ -49,14 +48,12 @@ class SchemaDefinitionError(

}

class SchemaDefinitionErrorFromWarning(
sdw: SchemaDefinitionDiagnosticBase,
warnID: WarnID
) extends SchemaDefinitionWarning(
warnID,
sdw.sc.toScalaOption,
sdw.annotationContext.toScalaOption,
(if (sdw.mfmt.isDefined) sdw.mfmt.get else ""),
class SchemaDefinitionErrorFromWarning(sdw: SchemaDefinitionWarning)
extends SchemaDefinitionWarning(
sdw.warnID,
sdw.schemaContext,
sdw.annotationContext,
sdw.kind,
sdw.args: _*
) {

Expand All @@ -74,60 +71,41 @@ class RelativePathPastRootError(schemaContext: SchemaFileLocation, kind: String,

class RuntimeSchemaDefinitionError(
schemaContext: SchemaFileLocation,
runtimeContext: ParseOrUnparseState,
causedBy: Throwable,
fmtString: String,
args: Any*
) extends SchemaDefinitionDiagnosticBase(
Maybe(schemaContext),
(runtimeContext match { // TODO: this is ugly.
case cs: CompileState => Nope
case _ => Maybe(runtimeContext)
}),
) extends SchemaDefinitionError(
Option(schemaContext),
None,
Maybe(causedBy),
Maybe(fmtString),
fmtString,
args: _*
) {

override def isError = true
override def modeName = "Runtime Schema Definition"

def this(
schemaContext: SchemaFileLocation,
runtimeContext: ParseOrUnparseState,
fmtString: String,
args: Any*
) =
this(schemaContext, runtimeContext, null, fmtString, args: _*)
}

class RuntimeSchemaDefinitionWarning(
warnID: WarnID,
schemaContext: SchemaFileLocation,
runtimeContext: ParseOrUnparseState,
kind: String,
args: Any*
) extends SchemaDefinitionDiagnosticBase(
) extends SchemaDefinitionWarning(
warnID,
Some(schemaContext),
Some(runtimeContext),
None,
Nope,
Maybe(kind + s" (id: ${warnID})"),
kind,
args: _*
) {

override def isError = false
override def modeName = "Runtime Schema Definition"

}

class SchemaDefinitionWarning(
warnID: WarnID,
schemaContext: Option[SchemaFileLocation],
annotationContext: Option[SchemaFileLocation],
kind: String,
args: Any*
val warnID: WarnID,
val schemaContext: Option[SchemaFileLocation],
val annotationContext: Option[SchemaFileLocation],
val kind: String,
val args: Any*
) extends SchemaDefinitionDiagnosticBase(
schemaContext,
None,
Expand Down Expand Up @@ -207,12 +185,12 @@ final class TunableLimitExceededError(
}

abstract class SchemaDefinitionDiagnosticBase(
val sc: Maybe[SchemaFileLocation],
sc: Maybe[SchemaFileLocation],
runtimeContext: Maybe[ParseOrUnparseState],
val annotationContext: Option[SchemaFileLocation],
private val annotationContext: Option[SchemaFileLocation],
mc: Maybe[Throwable],
val mfmt: Maybe[String],
val args: Any*
mfmt: Maybe[String],
args: Any*
) extends Diagnostic(
sc,
if (runtimeContext.isDefined) Maybe(runtimeContext.get.currentLocation) else Nope,
Expand Down Expand Up @@ -286,7 +264,7 @@ trait ImplementsThrowsOrSavesSDE extends ImplementsThrowsSDE with SavesErrorsAnd
args: _*
)
if (tunable.escalateWarningsToErrors) {
val sde = new SchemaDefinitionErrorFromWarning(sdw, warnID)
val sde = new SchemaDefinitionErrorFromWarning(sdw)
toss(sde)
} else {
warn(sdw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,6 @@ class DataProcessor(
state.dataInputStream.inputSource.setInvalid
state.setFailed(sde)
}
case rsde: RuntimeSchemaDefinitionError => {
state.dataInputStream.inputSource.setInvalid
state.setFailed(rsde)
}
case sdefw: SchemaDefinitionErrorFromWarning => {
state.dataInputStream.inputSource.setInvalid
state.setFailed(sdefw)
Expand Down Expand Up @@ -578,8 +574,8 @@ class DataProcessor(
unparserState.setFailed(sde)
unparserState.unparseResult
}
case rsde: RuntimeSchemaDefinitionError => {
unparserState.setFailed(rsde)
case sdefw: SchemaDefinitionErrorFromWarning => {
unparserState.setFailed(sdefw)
unparserState.unparseResult
}
case e: ErrorAlreadyHandled => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,13 @@ abstract class ParseOrUnparseState protected (

final def SDE(str: String, args: Any*) = {
val ctxt = getContext()
val rsde = new RuntimeSchemaDefinitionError(ctxt.schemaFileLocation, this, str, args: _*)
val rsde = new RuntimeSchemaDefinitionError(ctxt.schemaFileLocation, str, args: _*)
ctxt.toss(rsde)
}

final def SDEButContinue(str: String, args: Any*) = {
val ctxt = getContext()
val rsde = new RuntimeSchemaDefinitionError(ctxt.schemaFileLocation, this, str, args: _*)
val rsde = new RuntimeSchemaDefinitionError(ctxt.schemaFileLocation, str, args: _*)
diagnostics = rsde :: diagnostics
}

Expand All @@ -574,9 +574,9 @@ abstract class ParseOrUnparseState protected (
tssdw.contains(warnID) || tssdw.contains(WarnID.All)
if (!suppress) {
val rsdw =
new RuntimeSchemaDefinitionWarning(warnID, ctxt.schemaFileLocation, this, str, args: _*)
new RuntimeSchemaDefinitionWarning(warnID, ctxt.schemaFileLocation, str, args: _*)
if (tunable.escalateWarningsToErrors) {
val sde = new SchemaDefinitionErrorFromWarning(rsdw, warnID)
val sde = new SchemaDefinitionErrorFromWarning(rsdw)
ctxt.toss(sde)
} else {
diagnostics = rsdw :: diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ class SuspensionTracker(suspensionWaitYoung: Int, suspensionWaitOld: Int) {
class SuspensionDeadlockException(suspExprs: Seq[Suspension])
extends RuntimeSchemaDefinitionError(
suspExprs(0).rd.schemaFileLocation,
suspExprs(0).savedUstate,
"Expressions/Unparsers are circularly deadlocked (mutually defined):\n%s",
suspExprs.groupBy { _.rd }.mapValues { _(0) }.values.mkString(" - ", "\n - ", "")
suspExprs
.groupBy {
_.rd
}
.mapValues {
_(0)
}
.values
.mkString(" - ", "\n - ", "")
)
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,7 @@ trait DoSDEMixin {
throw sde
}
case other => {
val sde = new RuntimeSchemaDefinitionError(
state.getContext().schemaFileLocation,
state,
e,
null
)
val sde = new RuntimeSchemaDefinitionError(state.getContext().schemaFileLocation, e.getMessage)
state.setFailed(sde)
state.toss(sde)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ case class NotParsableParser(context: ElementRuntimeData) extends PrimParserNoDa
// create an SDE and toss it
val rsde = new RuntimeSchemaDefinitionError(
context.schemaFileLocation,
state,
"This schema was compiled without parse support. Check the parseUnparsePolicy tunable or dfdlx:parseUnparsePolicy property."
)
context.toss(rsde)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ case class NotUnparsableUnparser(override val context: ElementRuntimeData)
// create an SDE and toss it
val rsde = new RuntimeSchemaDefinitionError(
context.schemaFileLocation,
state,
"This schema was compiled without unparse support. Check the parseUnparsePolicy tunable or dfdlx:parseUnparsePolicy property."
)
context.toss(rsde)
Expand Down

0 comments on commit 175471a

Please sign in to comment.