diff --git a/plugins/todo-backend/package.json b/plugins/todo-backend/package.json index 888c9a404c..fd9bc46e1e 100644 --- a/plugins/todo-backend/package.json +++ b/plugins/todo-backend/package.json @@ -28,6 +28,7 @@ "@backstage/catalog-client": "^0.3.6", "@backstage/catalog-model": "^0.7.3", "@backstage/config": "^0.1.3", + "@backstage/errors": "^0.1.1", "@backstage/integration": "^0.5.0", "@types/express": "^4.17.6", "cross-fetch": "^3.0.6", diff --git a/plugins/todo-backend/src/service/TodoReaderService.ts b/plugins/todo-backend/src/service/TodoReaderService.ts index bc8ebbcd22..66ac0dbf4c 100644 --- a/plugins/todo-backend/src/service/TodoReaderService.ts +++ b/plugins/todo-backend/src/service/TodoReaderService.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { InputError, NotFoundError } from '@backstage/backend-common'; +import { InputError, NotFoundError } from '@backstage/errors'; import { CatalogClient } from '@backstage/catalog-client'; import { LOCATION_ANNOTATION, diff --git a/plugins/todo-backend/src/service/router.ts b/plugins/todo-backend/src/service/router.ts index 1132b879c4..d4f1a49c70 100644 --- a/plugins/todo-backend/src/service/router.ts +++ b/plugins/todo-backend/src/service/router.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { InputError } from '@backstage/backend-common'; import { EntityName, parseEntityName } from '@backstage/catalog-model'; +import { InputError } from '@backstage/errors'; import express from 'express'; import Router from 'express-promise-router'; import { TodoService } from './types'; diff --git a/plugins/todo/package.json b/plugins/todo/package.json index d940dfbe8a..91802b7b4a 100644 --- a/plugins/todo/package.json +++ b/plugins/todo/package.json @@ -28,6 +28,7 @@ "dependencies": { "@backstage/catalog-model": "^0.7.3", "@backstage/core": "^0.7.0", + "@backstage/errors": "^0.1.1", "@backstage/plugin-catalog-react": "^0.1.1", "@backstage/theme": "^0.2.3", "@material-ui/core": "^4.11.0", diff --git a/plugins/todo/src/api/TodoClient.ts b/plugins/todo/src/api/TodoClient.ts index ca5284537b..07b5eabead 100644 --- a/plugins/todo/src/api/TodoClient.ts +++ b/plugins/todo/src/api/TodoClient.ts @@ -15,6 +15,7 @@ */ import { serializeEntityRef } from '@backstage/catalog-model'; +import { ResponseError } from '@backstage/errors'; import { DiscoveryApi, IdentityApi } from '@backstage/core'; import { TodoApi, TodoListOptions, TodoListResult } from './types'; @@ -64,39 +65,10 @@ export class TodoClient implements TodoApi { }); if (!res.ok) { - const error = await this.readResponseError(res, 'list todos'); - throw error; + throw await ResponseError.fromResponse(res); } const data: TodoListResult = await res.json(); return data; } - - private async readResponseError(res: Response, action: string) { - const error = new Error() as Error & { status: number }; - error.status = res.status; - - try { - const json = await res.clone().json(); - if (typeof json?.error?.message !== 'string') { - throw new Error('invalid error'); - } - error.message = json.error.message; - if (json.error.name) { - error.name = json.error.name; - } - } catch { - try { - const text = await res.text(); - error.message = `Failed to ${action}, ${text}`; - } catch { - error.message = `Failed to ${action}, status ${res.status}`; - } - if (res.status === 404) { - error.name = 'NotFoundError'; - } - } - - throw error; - } } diff --git a/plugins/todo/src/components/TodoList/TodoList.tsx b/plugins/todo/src/components/TodoList/TodoList.tsx index a18e8f8626..999aa4fcbe 100644 --- a/plugins/todo/src/components/TodoList/TodoList.tsx +++ b/plugins/todo/src/components/TodoList/TodoList.tsx @@ -20,9 +20,9 @@ import { useApi, OverflowTooltip, Link, + ResponseErrorPanel, } from '@backstage/core'; import { useEntity } from '@backstage/plugin-catalog-react'; -import Alert from '@material-ui/lab/Alert'; import React, { useState } from 'react'; import { todoApiRef } from '../../api'; import { TodoItem, TodoListOptions } from '../../api/types'; @@ -64,7 +64,7 @@ export const TodoList = () => { const [error, setError] = useState(); if (error) { - return {error.message}; + return ; } return (