Merge pull request #16449 from drodil/catalog_fetch_fail

fix: catalog:fetch to throw error for null entity
This commit is contained in:
Fredrik Adelöw
2023-02-20 15:22:43 +01:00
committed by GitHub
3 changed files with 29 additions and 1 deletions
@@ -90,7 +90,7 @@ describe('catalog:fetch', () => {
expect(mockContext.output).toHaveBeenCalledWith('entity', null);
});
it('should throw error if entity not in catalog and optional is false', async () => {
it('should throw error if entity fetch fails from catalog and optional is false', async () => {
getEntityByRef.mockImplementationOnce(() => {
throw new Error('Not found');
});
@@ -109,4 +109,22 @@ describe('catalog:fetch', () => {
});
expect(mockContext.output).not.toHaveBeenCalled();
});
it('should throw error if entity not in catalog and optional is false', async () => {
getEntityByRef.mockReturnValueOnce(null);
await expect(
action.handler({
...mockContext,
input: {
entityRef: 'component:default/test',
},
}),
).rejects.toThrow('Entity component:default/test not found');
expect(getEntityByRef).toHaveBeenCalledWith('component:default/test', {
token: 'secret',
});
expect(mockContext.output).not.toHaveBeenCalled();
});
});
@@ -93,6 +93,11 @@ export function createFetchCatalogEntityAction(options: {
throw e;
}
}
if (!entity && !optional) {
throw new Error(`Entity ${entityRef} not found`);
}
ctx.output('entity', entity ?? null);
},
});