From 5c9ee908bde21be84e6d9629022a96df7e12683f Mon Sep 17 00:00:00 2001 From: Michael Watson Date: Thu, 6 Jul 2023 11:16:05 -0700 Subject: [PATCH] Handle missing node locations in composition error (#362) Fixes #361 Co-authored-by: Avery Harnish --- federation-1/harmonizer/js-src/composition.ts | 13 ++++---- federation-2/harmonizer/js-src/composition.ts | 30 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/federation-1/harmonizer/js-src/composition.ts b/federation-1/harmonizer/js-src/composition.ts index 96ee41a36..e2af6f476 100644 --- a/federation-1/harmonizer/js-src/composition.ts +++ b/federation-1/harmonizer/js-src/composition.ts @@ -37,12 +37,15 @@ export function composition( composed.errors.map((err) => { let nodes: BuildErrorNode[] = []; err.nodes.map((node) => { - nodes.push({ + let n: BuildErrorNode = { subgraph: (node as any)?.subgraph, - source: node?.loc.source.body, - start: getPosition(node.loc.startToken), - end: getPosition(node.loc.endToken), - }); + }; + if (node.loc) { + n.source = node?.loc?.source?.body; + n.start = getPosition(node.loc.startToken); + n.end = getPosition(node.loc.endToken); + } + nodes.push(n); }); errors.push({ diff --git a/federation-2/harmonizer/js-src/composition.ts b/federation-2/harmonizer/js-src/composition.ts index 6e01bbd6f..be59ba2f9 100644 --- a/federation-2/harmonizer/js-src/composition.ts +++ b/federation-2/harmonizer/js-src/composition.ts @@ -38,14 +38,7 @@ export function composition( if (composed.hints) { composed.hints.map((composed_hint) => { let nodes: BuildErrorNode[] = []; - composed_hint.nodes?.map((node) => { - nodes.push({ - subgraph: (node as any)?.subgraph, - source: node?.loc.source.body, - start: getPosition(node.loc.startToken), - end: getPosition(node.loc.endToken), - }); - }); + composed_hint.nodes?.map((node) => nodes.push(getBuildErrorNode(node))); hints.push({ message: composed_hint.toString(), @@ -60,14 +53,7 @@ export function composition( let errors: CompositionError[] = []; composed.errors.map((err) => { let nodes: BuildErrorNode[] = []; - err.nodes?.map((node) => { - nodes.push({ - subgraph: (node as any)?.subgraph, - source: node?.loc.source.body, - start: getPosition(node.loc.startToken), - end: getPosition(node.loc.endToken), - }); - }); + err.nodes?.map((node) => nodes.push(getBuildErrorNode(node))); errors.push({ code: (err?.extensions["code"] as string) ?? "", @@ -86,6 +72,18 @@ export function composition( }; } +function getBuildErrorNode(node: ASTNode) { + let n: BuildErrorNode = { + subgraph: (node as any)?.subgraph, + }; + if (node.loc) { + n.source = node.loc?.source?.body; + n.start = getPosition(node.loc.startToken); + n.end = getPosition(node.loc.endToken); + } + return n; +} + function getPosition(token: Token): Position { return { start: token.start,