Skip to content

Commit be3d0e4

Browse files
authored
Fix crash when referencing non-existent target ref ID (#128)
The handleRefTargets function used Array.find() with a non-null assertion to look up target references by refId. When a non-existent ref was referenced (e.g., #Z when only #A and #B exist), find() returned undefined and accessing .target on it caused a runtime TypeError. This change adds proper error handling to throw a descriptive error message that includes the invalid ref and lists all available refs, making debugging much easier for users.
1 parent 054cbbb commit be3d0e4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/panels/graph.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,15 @@ function handleRefTargets(
158158
): { targetFull?: string | undefined } {
159159
if (target.includes('#')) {
160160
const refs = getRefsFromTarget(target);
161-
const findTargetByRefId = (targets: TargetObject[], refId: string) =>
162-
targets.find((target) => target.refId === refId)!.target;
161+
const findTargetByRefId = (targets: TargetObject[], refId: string) => {
162+
const found = targets.find((target) => target.refId === refId);
163+
if (!found) {
164+
throw new Error(
165+
`Invalid target reference: #${refId} does not exist. Available refs: ${targets.map((t) => t.refId).join(', ')}`
166+
);
167+
}
168+
return found.target;
169+
};
163170

164171
return {
165172
targetFull: refs.reduce(

test/panels/graph.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,14 @@ test('graph should add other targets when a target contains refs', () => {
142142

143143
expect(actualDivideTarget).toEqual(expectedDivideTarget);
144144
});
145+
146+
test('graph should throw descriptive error when referencing non-existent target', () => {
147+
expect(() => {
148+
new Graph({
149+
targets: [
150+
'target-1',
151+
"alias(divideSeries(#Z, #A), 'invalid ref')", // #Z doesn't exist
152+
],
153+
});
154+
}).toThrow(/Invalid target reference: #Z does not exist/);
155+
});

0 commit comments

Comments
 (0)