Skip to content

Commit

Permalink
Improved the error handling for constraint message replacements using…
Browse files Browse the repository at this point in the history
… Metapath expressions. Resolves metaschema-framework#289.
  • Loading branch information
david-waltermire committed Dec 12, 2024
1 parent deb73be commit 37f4980
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ public ISequence<?> execute(
result = executeInternal(convertedArguments, dynamicContext, contextItem);

if (callingContext != null) {
// ensure the result sequence is list backed
result.getValue();
// add result to cache
dynamicContext.cacheResult(callingContext, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,28 +676,32 @@ private void validateExpect(
@NonNull INodeItem node,
@NonNull ISequence<? extends INodeItem> targets,
@NonNull DynamicContext dynamicContext) {
IMetapathExpression metapath = IMetapathExpression.compile(
constraint.getTest(),
dynamicContext.getStaticContext());
try {
IMetapathExpression metapath = IMetapathExpression.compile(
constraint.getTest(),
dynamicContext.getStaticContext());

IConstraintValidationHandler handler = getConstraintValidationHandler();
targets.stream()
.forEachOrdered(item -> {
assert item != null;
IConstraintValidationHandler handler = getConstraintValidationHandler();
targets.stream()
.forEachOrdered(item -> {
assert item != null;

if (item.hasValue()) {
try {
ISequence<?> result = metapath.evaluate(item, dynamicContext);
if (FnBoolean.fnBoolean(result).toBoolean()) {
handlePass(constraint, node, item, dynamicContext);
} else {
handler.handleExpectViolation(constraint, node, item, dynamicContext);
if (item.hasValue()) {
try {
ISequence<?> result = metapath.evaluate(item, dynamicContext);
if (FnBoolean.fnBoolean(result).toBoolean()) {
handlePass(constraint, node, item, dynamicContext);
} else {
handler.handleExpectViolation(constraint, node, item, dynamicContext);
}
} catch (MetapathException ex) {
handleError(constraint, item, ex, dynamicContext);
}
} catch (MetapathException ex) {
handleError(constraint, item, ex, dynamicContext);
}
}
});
});
} catch (MetapathException ex) {
handleError(constraint, node, ex, dynamicContext);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
import gov.nist.secauto.metaschema.core.metapath.IMetapathExpression;
import gov.nist.secauto.metaschema.core.metapath.MetapathException;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.model.IAttributable;
import gov.nist.secauto.metaschema.core.model.ISource;
Expand Down Expand Up @@ -95,8 +96,16 @@ public String generateMessage(@NonNull INodeItem item, @NonNull DynamicContext c

return ObjectUtils.notNull(ReplacementScanner.replaceTokens(message, METAPATH_VALUE_TEMPLATE_PATTERN, match -> {
String metapath = ObjectUtils.notNull(match.group(2));
IMetapathExpression expr = IMetapathExpression.compile(metapath, context.getStaticContext());
return expr.evaluateAs(item, IMetapathExpression.ResultType.STRING, context);
try {
IMetapathExpression expr = IMetapathExpression.compile(metapath, context.getStaticContext());
return expr.evaluateAs(item, IMetapathExpression.ResultType.STRING, context);
} catch (MetapathException ex) {
throw new MetapathException(
String.format("Unable to evaluate the message replacement expression '%s'. %s",
metapath,
ex.getLocalizedMessage()),
ex);
}
}).toString());
}
}

0 comments on commit 37f4980

Please sign in to comment.