todo,todo-backend: use @backstage/errors
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Error>();
|
||||
|
||||
if (error) {
|
||||
return <Alert severity="error">{error.message}</Alert>;
|
||||
return <ResponseErrorPanel error={error} />;
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user