Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Snapshot + Restore] Fix error handling for repositories API #103723

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+++++++++++++++++++++++++++


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