fix error handling for repositories API (#103723)

This commit is contained in:
Alison Goryachev 2021-06-29 20:13:31 -04:00 committed by GitHub
parent 25db1df1a3
commit 57a91215f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -188,11 +188,10 @@ describe('[Snapshot and Restore API Routes] Repositories', () => {
const mockEsResponse = {
[name]: { type: '', settings: {} },
};
const mockEsSnapshotError = jest.fn().mockRejectedValueOnce(new Error('snapshot error'));
getClusterSettingsFn.mockResolvedValue({ body: mockSnapshotGetManagedRepositoryEsResponse });
getRepoFn.mockResolvedValue({ body: mockEsResponse });
getSnapshotFn.mockResolvedValue({ body: mockEsSnapshotError });
getSnapshotFn.mockRejectedValueOnce(new Error('snapshot error'));
const expectedResponse = {
repository: { name, ...mockEsResponse[name] },

View file

@ -6,7 +6,10 @@
*/
import { TypeOf } from '@kbn/config-schema';
import type { SnapshotRepositorySettings } from '@elastic/elasticsearch/api/types';
import type {
SnapshotGetRepositoryResponse,
SnapshotRepositorySettings,
} from '@elastic/elasticsearch/api/types';
import { DEFAULT_REPOSITORY_TYPES, REPOSITORY_PLUGINS_MAP } from '../../../common/constants';
import { Repository, RepositoryType } from '../../../common/types';
@ -101,7 +104,7 @@ export function registerRepositoriesRoutes({
const managedRepository = await getManagedRepositoryName(clusterClient.asCurrentUser);
let repositoryByName: any;
let repositoryByName: SnapshotGetRepositoryResponse;
try {
({ body: repositoryByName } = await clusterClient.asCurrentUser.snapshot.getRepository({
@ -111,12 +114,18 @@ export function registerRepositoriesRoutes({
return handleEsError({ error: e, response: res });
}
const response = await clusterClient.asCurrentUser.snapshot.get({
repository: name,
snapshot: '_all',
});
const { snapshots: snapshotList } = response.body;
const {
body: { snapshots: snapshotList },
} = await clusterClient.asCurrentUser.snapshot
.get({
repository: name,
snapshot: '_all',
})
.catch((e) => ({
body: {
snapshots: null,
},
}));
if (repositoryByName[name]) {
const { type = '', settings = {} } = repositoryByName[name];