Skip to content

Commit

Permalink
Re-throw fhirpath error with details about linkId. Closes #80
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed Oct 9, 2024
1 parent 7e13466 commit fc07701
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
36 changes: 22 additions & 14 deletions sdc-qrf/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,28 @@ export function QuestionItem(props: QuestionItemProps) {
if (!isGroupItem(questionItem, context) && calculatedExpression) {
// TODO: Add support for x-fhir-query
if (calculatedExpression.language === 'text/fhirpath') {
const newValues = fhirpath.evaluate(
context.context || {},
calculatedExpression.expression!,
context as ItemContext,
);
const newAnswers = newValues.length
? repeats
? newValues.map((answer: any) => ({ value: wrapAnswerValue(type, answer) }))
: [{ value: wrapAnswerValue(type, newValues[0]) }]
: undefined;

if (!isEqual(newAnswers, prevAnswers)) {
const allValues = _.set(_.cloneDeep(formValues), fieldPath, newAnswers);
setFormValues(allValues, fieldPath, newAnswers);
try {
const newValues = fhirpath.evaluate(
context.context || {},
calculatedExpression.expression!,
context as ItemContext,
);
const newAnswers = newValues.length
? repeats
? newValues.map((answer: any) => ({
value: wrapAnswerValue(type, answer),
}))
: [{ value: wrapAnswerValue(type, newValues[0]) }]
: undefined;

if (!isEqual(newAnswers, prevAnswers)) {
const allValues = _.set(_.cloneDeep(formValues), fieldPath, newAnswers);
setFormValues(allValues, fieldPath, newAnswers);
}
} catch (err: unknown) {
throw Error(
`FHIRPath expression evaluation failure for "calculatedExpression" in ${questionItem.linkId}: ${err}`,
);
}
}
}
Expand Down
60 changes: 37 additions & 23 deletions sdc-qrf/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,25 @@ export function calcContext(
qrItem: QuestionnaireResponseItem,
): ItemContext {
// TODO: add root variable support
return {
...(variables || []).reduce(
(acc, curVariable) => ({
...acc,
[curVariable.name!]: fhirpath.evaluate(qrItem || {}, curVariable.expression!, acc),
}),
{ ...initialContext, context: qrItem, qitem: qItem },
),
};
try {
return {
...(variables || []).reduce(
(acc, curVariable) => ({
...acc,
[curVariable.name!]: fhirpath.evaluate(
qrItem || {},
curVariable.expression!,
acc,
),
}),
{ ...initialContext, context: qrItem, qitem: qItem },
),
};
} catch (err: unknown) {
throw Error(
`FHIRPath expression evaluation failure for "variable" in ${qItem.linkId}: ${err}`,
);
}
}

export function compareValue(firstAnswerValue: AnswerValue, secondAnswerValue: AnswerValue) {
Expand Down Expand Up @@ -501,21 +511,25 @@ function isQuestionEnabled(args: IsQuestionEnabledArgs) {
}

if (enableWhenExpression && enableWhenExpression.language === 'text/fhirpath') {
const expressionResult = fhirpath.evaluate(
args.context.resource,
enableWhenExpression.expression!,
args.context ?? {},
)[0];

if (typeof expressionResult !== 'boolean') {
throw Error(`
linkId: ${args.qItem.linkId}
Expression result: ${expressionResult}
The result of enableWhenExpression is not a boolean value
`);
}
try {
const expressionResult = fhirpath.evaluate(
args.context.resource,
enableWhenExpression.expression!,
args.context ?? {},
)[0];

if (typeof expressionResult !== 'boolean') {
throw Error(
`The result of enableWhenExpression is not a boolean value. Expression result: ${expressionResult}`,
);
}

return expressionResult;
return expressionResult;
} catch (err: unknown) {
throw Error(
`FHIRPath expression evaluation failure for "enableWhenExpression" in ${args.qItem.linkId}: ${err}`,
);
}
}

const iterFn = enableBehavior === 'any' ? _.some : _.every;
Expand Down

0 comments on commit fc07701

Please sign in to comment.