Skip to content

Commit

Permalink
fix error handling for repositories API (#103723) (#103814)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Jun 30, 2021
1 parent 12c16b7 commit 3cfaee7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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] },
Expand Down
25 changes: 17 additions & 8 deletions x-pack/plugins/snapshot_restore/server/routes/api/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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({
Expand All @@ -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];
Expand Down

0 comments on commit 3cfaee7

Please sign in to comment.