diff --git a/.changeset/techdocs-green-rabbits-burn.md b/.changeset/techdocs-green-rabbits-burn.md
new file mode 100644
index 0000000000..5af68c2000
--- /dev/null
+++ b/.changeset/techdocs-green-rabbits-burn.md
@@ -0,0 +1,5 @@
+---
+'@backstage/plugin-techdocs': patch
+---
+
+Enhance API calls to support trapping 500 errors from techdocs-backend
diff --git a/plugins/techdocs/src/api.ts b/plugins/techdocs/src/api.ts
index a8b1f1e34f..7f77be44e2 100644
--- a/plugins/techdocs/src/api.ts
+++ b/plugins/techdocs/src/api.ts
@@ -157,14 +157,23 @@ export class TechDocsStorageApi implements TechDocsStorage {
`${url.endsWith('/') ? url : `${url}/`}index.html`,
);
- if (request.status === 404) {
- let errorMessage = 'Page not found. ';
- // path is empty for the home page of an entity's docs site
- if (!path) {
- errorMessage +=
- 'This could be because there is no index.md file in the root of the docs directory of this repository.';
- }
- throw new Error(errorMessage);
+ let errorMessage = '';
+ switch (request.status) {
+ case 404:
+ errorMessage = 'Page not found. ';
+ // path is empty for the home page of an entity's docs site
+ if (!path) {
+ errorMessage +=
+ 'This could be because there is no index.md file in the root of the docs directory of this repository.';
+ }
+ throw new Error(errorMessage);
+ case 500:
+ errorMessage =
+ 'Could not generate documentation or an error in the TechDocs backend. ';
+ throw new Error(errorMessage);
+ default:
+ // Do nothing
+ break;
}
return request.text();
diff --git a/plugins/techdocs/src/reader/components/Reader.tsx b/plugins/techdocs/src/reader/components/Reader.tsx
index 50990911a1..c651f1ec13 100644
--- a/plugins/techdocs/src/reader/components/Reader.tsx
+++ b/plugins/techdocs/src/reader/components/Reader.tsx
@@ -155,7 +155,9 @@ export const Reader = ({ entityId, onReady }: Props) => {
]);
if (error) {
- return ;
+ // TODO Enhance API call to return customize error objects so we can identify which we ran into
+ // For now this defaults to display error code 404
+ return ;
}
return (
diff --git a/plugins/techdocs/src/reader/components/TechDocsNotFound.test.tsx b/plugins/techdocs/src/reader/components/TechDocsNotFound.test.tsx
index d3c98fec78..c6562ec8ad 100644
--- a/plugins/techdocs/src/reader/components/TechDocsNotFound.test.tsx
+++ b/plugins/techdocs/src/reader/components/TechDocsNotFound.test.tsx
@@ -41,3 +41,20 @@ describe('', (
expect(rendered.getByTestId('go-back-link')).toBeDefined();
});
});
+
+describe('', () => {
+ it('should render with a custom status code, custom error message and go back link', () => {
+ const rendered = render(
+ wrapInTestApp(
+ ,
+ ),
+ );
+ rendered.getByText(/This is a custom error message/i);
+ rendered.getByText(/500/i);
+ rendered.getByText(/Looks like someone dropped the mic!/i);
+ expect(rendered.getByTestId('go-back-link')).toBeDefined();
+ });
+});
diff --git a/plugins/techdocs/src/reader/components/TechDocsNotFound.tsx b/plugins/techdocs/src/reader/components/TechDocsNotFound.tsx
index cdacc1cb7e..04f2106161 100644
--- a/plugins/techdocs/src/reader/components/TechDocsNotFound.tsx
+++ b/plugins/techdocs/src/reader/components/TechDocsNotFound.tsx
@@ -19,9 +19,10 @@ import { ErrorPage, useApi, configApiRef } from '@backstage/core';
type Props = {
errorMessage?: string;
+ statusCode?: number;
};
-export const TechDocsNotFound = ({ errorMessage }: Props) => {
+export const TechDocsNotFound = ({ errorMessage, statusCode }: Props) => {
const techdocsBuilder = useApi(configApiRef).getOptionalString(
'techdocs.builder',
);
@@ -37,7 +38,7 @@ export const TechDocsNotFound = ({ errorMessage }: Props) => {
return (