diff --git a/.changeset/dull-tables-joke.md b/.changeset/dull-tables-joke.md
new file mode 100644
index 0000000000..e987377023
--- /dev/null
+++ b/.changeset/dull-tables-joke.md
@@ -0,0 +1,5 @@
+---
+'@backstage/plugin-catalog': patch
+---
+
+Fixed an issue where `EntitySwitch` was preventing the display of entity errors.
diff --git a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx
index c93922e938..d9998b0ca5 100644
--- a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx
+++ b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.test.tsx
@@ -59,6 +59,50 @@ describe('EntitySwitch', () => {
expect(screen.queryByText('C')).not.toBeInTheDocument();
});
+ it('should render the default case if entity is not found', () => {
+ const content = (
+
+
+
+
+
+ );
+
+ render(
+
+
+ {content}
+
+ ,
+ );
+
+ expect(screen.queryByText('A')).not.toBeInTheDocument();
+ expect(screen.queryByText('B')).not.toBeInTheDocument();
+ expect(screen.queryByText('C')).toBeInTheDocument();
+ });
+
+ it(`shouldn't render any children if entity is loading and no entity exists in the context`, () => {
+ const content = (
+
+
+
+
+
+ );
+
+ render(
+
+
+ {content}
+
+ ,
+ );
+
+ expect(screen.queryByText('A')).not.toBeInTheDocument();
+ expect(screen.queryByText('B')).not.toBeInTheDocument();
+ expect(screen.queryByText('C')).not.toBeInTheDocument();
+ });
+
it('should render the fallback if no cases are matching', () => {
const content = (
@@ -170,7 +214,7 @@ describe('EntitySwitch', () => {
expect(screen.queryByText('A')).not.toBeInTheDocument();
expect(screen.queryByText('B')).not.toBeInTheDocument();
- expect(screen.queryByText('C')).not.toBeInTheDocument();
+ expect(screen.queryByText('C')).toBeInTheDocument();
});
it('should switch child when filters switch', () => {
diff --git a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx
index b12495052e..9eab743308 100644
--- a/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx
+++ b/plugins/catalog/src/components/EntitySwitch/EntitySwitch.tsx
@@ -64,7 +64,7 @@ export interface EntitySwitchProps {
/** @public */
export const EntitySwitch = (props: EntitySwitchProps) => {
- const { entity } = useAsyncEntity();
+ const { entity, loading } = useAsyncEntity();
const apis = useApiHolder();
const results = useElementFilter(
@@ -77,14 +77,21 @@ export const EntitySwitch = (props: EntitySwitchProps) => {
})
.getElements()
.flatMap((element: ReactElement) => {
- // If the entity is missing or there is an error, render nothing
- if (!entity) {
+ if (loading && !entity) {
return [];
}
const { if: condition, children: elementsChildren } =
element.props as EntitySwitchCase;
+ if (!entity) {
+ return [
+ {
+ if: condition === undefined,
+ children: elementsChildren,
+ },
+ ];
+ }
return [
{
if: condition?.(entity, { apis }),
@@ -92,7 +99,7 @@ export const EntitySwitch = (props: EntitySwitchProps) => {
},
];
}),
- [apis, entity],
+ [apis, entity, loading],
);
const hasAsyncCases = results.some(