Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/panels/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,15 @@ function handleRefTargets(
): { targetFull?: string | undefined } {
if (target.includes('#')) {
const refs = getRefsFromTarget(target);
const findTargetByRefId = (targets: TargetObject[], refId: string) =>
targets.find((target) => target.refId === refId)!.target;
const findTargetByRefId = (targets: TargetObject[], refId: string) => {
const found = targets.find((target) => target.refId === refId);
if (!found) {
throw new Error(
`Invalid target reference: #${refId} does not exist. Available refs: ${targets.map((t) => t.refId).join(', ')}`
);
}
return found.target;
};

return {
targetFull: refs.reduce(
Expand Down
11 changes: 11 additions & 0 deletions test/panels/graph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ test('graph should add other targets when a target contains refs', () => {

expect(actualDivideTarget).toEqual(expectedDivideTarget);
});

test('graph should throw descriptive error when referencing non-existent target', () => {
expect(() => {
new Graph({
targets: [
'target-1',
"alias(divideSeries(#Z, #A), 'invalid ref')", // #Z doesn't exist
],
});
}).toThrow(/Invalid target reference: #Z does not exist/);
});