Merge pull request #32501 from backstage/rugvip/attribute
frontend-app-api: better isolation and attribution of instantiation failures
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
---
|
||||
'@backstage/frontend-app-api': patch
|
||||
---
|
||||
|
||||
Updated error reporting and app tree resolution logic to attribute errors to the correct extension and allow app startup to proceed more optimistically:
|
||||
|
||||
- If an attachment fails to provide the required input data, the error is now attributed to the attachment rather than the parent extension.
|
||||
- Singleton extension inputs will now only forward attachment errors if the input is required.
|
||||
- Array extension inputs will now filter out failed attachments instead of failing the entire app tree resolution.
|
||||
@@ -1574,41 +1574,158 @@ describe('instantiateAppNodeTree', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('should refuse to create an instance with multiple inputs that did not provide required data', () => {
|
||||
const node = makeNode(
|
||||
resolveExtensionDefinition(
|
||||
createExtension({
|
||||
name: 'test',
|
||||
attachTo: { id: 'ignored', input: 'ignored' },
|
||||
inputs: {
|
||||
singleton: createExtensionInput([otherDataRef], {
|
||||
singleton: true,
|
||||
}),
|
||||
},
|
||||
output: [],
|
||||
factory: () => [],
|
||||
}),
|
||||
{ namespace: 'app' },
|
||||
),
|
||||
describe('with attachment failures', () => {
|
||||
const inputCountRef = createExtensionDataRef<number>().with({
|
||||
id: 'input-count',
|
||||
});
|
||||
|
||||
const attachmentWithoutRequiredData = makeInstanceWithId(
|
||||
simpleExtension,
|
||||
undefined,
|
||||
);
|
||||
expect(
|
||||
createAppNodeInstance({
|
||||
apis: testApis,
|
||||
attachments: new Map([
|
||||
['singleton', [makeInstanceWithId(simpleExtension, undefined)]],
|
||||
]),
|
||||
node,
|
||||
collector,
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(collector.collectErrors()).toEqual([
|
||||
{
|
||||
code: 'EXTENSION_INPUT_DATA_MISSING',
|
||||
message:
|
||||
"extension 'app/test' could not be attached because its output data ('test') does not match what the input 'singleton' requires ('other')",
|
||||
context: { node, inputName: 'singleton' },
|
||||
},
|
||||
]);
|
||||
|
||||
it('should proceed if input is optional', () => {
|
||||
const node = makeNode(
|
||||
resolveExtensionDefinition(
|
||||
createExtension({
|
||||
name: 'test',
|
||||
attachTo: { id: 'ignored', input: 'ignored' },
|
||||
inputs: {
|
||||
singleton: createExtensionInput([otherDataRef], {
|
||||
singleton: true,
|
||||
optional: true,
|
||||
}),
|
||||
},
|
||||
output: [inputCountRef],
|
||||
factory: ({ inputs }) => [
|
||||
inputCountRef(inputs.singleton ? 1 : 0),
|
||||
],
|
||||
}),
|
||||
{ namespace: 'app' },
|
||||
),
|
||||
);
|
||||
expect(
|
||||
createAppNodeInstance({
|
||||
apis: testApis,
|
||||
attachments: new Map([
|
||||
['singleton', [attachmentWithoutRequiredData]],
|
||||
]),
|
||||
node,
|
||||
collector,
|
||||
})?.getData(inputCountRef),
|
||||
).toBe(0);
|
||||
|
||||
expect(collector.collectErrors()).toEqual([
|
||||
{
|
||||
code: 'EXTENSION_INPUT_DATA_MISSING',
|
||||
message:
|
||||
"extension 'app/test' could not be attached because its output data ('test') does not match what the input 'singleton' requires ('other')",
|
||||
context: {
|
||||
node: attachmentWithoutRequiredData,
|
||||
inputName: 'singleton',
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should fail if input is required', () => {
|
||||
const node = makeNode(
|
||||
resolveExtensionDefinition(
|
||||
createExtension({
|
||||
name: 'test',
|
||||
attachTo: { id: 'ignored', input: 'ignored' },
|
||||
inputs: {
|
||||
singleton: createExtensionInput([otherDataRef], {
|
||||
singleton: true,
|
||||
}),
|
||||
},
|
||||
output: [inputCountRef],
|
||||
factory: ({ inputs }) => [
|
||||
inputCountRef(inputs.singleton ? 1 : 0),
|
||||
],
|
||||
}),
|
||||
{ namespace: 'app' },
|
||||
),
|
||||
);
|
||||
|
||||
expect(
|
||||
createAppNodeInstance({
|
||||
apis: testApis,
|
||||
attachments: new Map([
|
||||
['singleton', [attachmentWithoutRequiredData]],
|
||||
]),
|
||||
node,
|
||||
collector,
|
||||
})?.getData(inputCountRef),
|
||||
).toBeUndefined();
|
||||
|
||||
expect(collector.collectErrors()).toEqual([
|
||||
{
|
||||
code: 'EXTENSION_ATTACHMENT_MISSING',
|
||||
message:
|
||||
"input 'singleton' is required but it failed to be instantiated",
|
||||
context: { inputName: 'singleton', node },
|
||||
},
|
||||
{
|
||||
code: 'EXTENSION_INPUT_DATA_MISSING',
|
||||
message:
|
||||
"extension 'app/test' could not be attached because its output data ('test') does not match what the input 'singleton' requires ('other')",
|
||||
context: {
|
||||
node: attachmentWithoutRequiredData,
|
||||
inputName: 'singleton',
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should filter out failed attachments for non-singleton inputs', () => {
|
||||
const node = makeNode(
|
||||
resolveExtensionDefinition(
|
||||
createExtension({
|
||||
name: 'test',
|
||||
attachTo: { id: 'ignored', input: 'ignored' },
|
||||
inputs: {
|
||||
children: createExtensionInput([otherDataRef]),
|
||||
},
|
||||
output: [inputCountRef],
|
||||
factory: ({ inputs }) => [
|
||||
inputCountRef(inputs.children.length),
|
||||
],
|
||||
}),
|
||||
{ namespace: 'app' },
|
||||
),
|
||||
);
|
||||
expect(
|
||||
createAppNodeInstance({
|
||||
apis: testApis,
|
||||
attachments: new Map([
|
||||
[
|
||||
'children',
|
||||
[
|
||||
attachmentWithoutRequiredData,
|
||||
makeInstanceWithId(simpleExtension, {
|
||||
other: 42,
|
||||
}),
|
||||
],
|
||||
],
|
||||
]),
|
||||
node,
|
||||
collector,
|
||||
})?.getData(inputCountRef),
|
||||
).toBe(1);
|
||||
|
||||
expect(collector.collectErrors()).toEqual([
|
||||
{
|
||||
code: 'EXTENSION_INPUT_DATA_MISSING',
|
||||
message:
|
||||
"extension 'app/test' could not be attached because its output data ('test') does not match what the input 'children' requires ('other')",
|
||||
context: {
|
||||
node: attachmentWithoutRequiredData,
|
||||
inputName: 'children',
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -38,21 +38,22 @@ const INSTANTIATION_FAILED = new Error('Instantiation failed');
|
||||
function mapWithFailures<T, U>(
|
||||
iterable: Iterable<T>,
|
||||
callback: (item: T) => U,
|
||||
options?: { ignoreFailures?: boolean },
|
||||
): U[] {
|
||||
let failed = false;
|
||||
const results = Array.from(iterable).map(item => {
|
||||
const results = [];
|
||||
for (const item of iterable) {
|
||||
try {
|
||||
return callback(item);
|
||||
results.push(callback(item));
|
||||
} catch (error) {
|
||||
if (error === INSTANTIATION_FAILED) {
|
||||
failed = true;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
return null as any;
|
||||
}
|
||||
});
|
||||
if (failed) {
|
||||
}
|
||||
if (failed && !options?.ignoreFailures) {
|
||||
throw INSTANTIATION_FAILED;
|
||||
}
|
||||
return results;
|
||||
@@ -119,7 +120,7 @@ function resolveInputDataContainer(
|
||||
.map(r => `'${r.id}'`)
|
||||
.join(', ');
|
||||
|
||||
collector.report({
|
||||
collector.child({ node: attachment }).report({
|
||||
code: 'EXTENSION_INPUT_DATA_MISSING',
|
||||
message: `extension '${attachment.spec.id}' could not be attached because its output data (${provided}) does not match what the input '${inputName}' requires (${expected})`,
|
||||
});
|
||||
@@ -271,21 +272,37 @@ function resolveV2Inputs(
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
return resolveInputDataContainer(
|
||||
input.extensionData,
|
||||
attachedNodes[0],
|
||||
inputName,
|
||||
collector,
|
||||
);
|
||||
try {
|
||||
return resolveInputDataContainer(
|
||||
input.extensionData,
|
||||
attachedNodes[0],
|
||||
inputName,
|
||||
collector,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error === INSTANTIATION_FAILED) {
|
||||
if (input.config.optional) {
|
||||
return undefined;
|
||||
}
|
||||
collector.report({
|
||||
code: 'EXTENSION_ATTACHMENT_MISSING',
|
||||
message: `input '${inputName}' is required but it failed to be instantiated`,
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return mapWithFailures(attachedNodes, attachment =>
|
||||
resolveInputDataContainer(
|
||||
input.extensionData,
|
||||
attachment,
|
||||
inputName,
|
||||
collector,
|
||||
),
|
||||
return mapWithFailures(
|
||||
attachedNodes,
|
||||
attachment =>
|
||||
resolveInputDataContainer(
|
||||
input.extensionData,
|
||||
attachment,
|
||||
inputName,
|
||||
collector,
|
||||
),
|
||||
{ ignoreFailures: true },
|
||||
);
|
||||
}) as ResolvedExtensionInputs<{ [inputName in string]: ExtensionInput }>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user