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

[Workspace] update workspace not found message to generic message #9189

Merged
merged 2 commits into from
Jan 17, 2025
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
2 changes: 2 additions & 0 deletions changelogs/fragments/9189.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Update workspace not found message to generic message ([#9189](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9189))
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ describe(`duplicate saved objects among workspaces`, () => {
})
.expect(400);

expect(result.body.message).toMatchInlineSnapshot(
`"Get target workspace non-existen-workspace error: undefined"`
);
expect(result.body.message).toMatchInlineSnapshot(`"Get target workspace error: undefined"`);
});

it('duplicate unsupported objects', async () => {
Expand Down
38 changes: 37 additions & 1 deletion src/plugins/workspace/server/integration_tests/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ describe('workspace service api integration test', () => {
);
expect(getResult.body.result.name).toEqual(testWorkspace.name);
});

it('get when workspace not found', async () => {
const workspaceId = 'non-exist workspace id';
const getResult = await osdTestServer.request.get(root, `/api/workspaces/${workspaceId}`);
expect(getResult.body.success).toEqual(false);
expect(getResult.body.error).toEqual('workspace not found');
});

it('update', async () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
Expand Down Expand Up @@ -185,6 +193,23 @@ describe('workspace service api integration test', () => {
expect(getResult.body.result.name).toEqual('updated');
});

it('update non exist workspace', async () => {
const workspaceId = 'non-exist workspace id';

const result = await osdTestServer.request
.put(root, `/api/workspaces/${workspaceId}`)
.send({
attributes: {
...omitId(testWorkspace),
name: 'updated',
},
})
.expect(200);

expect(result.body.success).toEqual(false);
expect(result.body.error).toEqual('workspace not found');
});

it('update workspace failed when new name is duplicate', async () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
Expand Down Expand Up @@ -286,6 +311,17 @@ describe('workspace service api integration test', () => {
`Reserved workspace ${result.body.result.id} is not allowed to delete.`
);
});

it('delete non exist workspace', async () => {
const workspaceId = 'non-exist workspace id';
const result = await osdTestServer.request
.delete(root, `/api/workspaces/${workspaceId}`)
.expect(200);

expect(result.body.success).toEqual(false);
expect(result.body.error).toEqual('workspace not found');
});

it('list', async () => {
await osdTestServer.request
.post(root, `/api/workspaces`)
Expand Down Expand Up @@ -504,7 +540,7 @@ describe('workspace service api integration test', () => {
.expect(400);

expect(result.body.message).toMatchInlineSnapshot(
`"Get target workspace test_workspace error: Saved object [workspace/test_workspace] not found"`
`"Get target workspace error: workspace not found"`
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/workspace/server/routes/duplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const registerDuplicateRoute = (
if (!getTargetWorkspaceResult.success) {
return res.badRequest({
body: {
message: `Get target workspace ${targetWorkspace} error: ${getTargetWorkspaceResult.error}`,
message: `Get target workspace error: ${getTargetWorkspaceResult.error}`,
},
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
WORKSPACE_TYPE,
Logger,
WorkspaceFindOptions,
SavedObjectsErrorHelpers,
} from '../../../core/server';
import { updateWorkspaceState, getWorkspaceState } from '../../../core/server/utils';
import {
Expand All @@ -39,6 +40,10 @@
defaultMessage: 'workspace name has already been used, try with a different name',
});

const WORKSPACE_NOT_FOUND_ERROR = i18n.translate('workspace.notFound.error', {
defaultMessage: 'workspace not found',
});

export class WorkspaceClient implements IWorkspaceClientImpl {
private setupDep: CoreSetup;
private logger: Logger;
Expand Down Expand Up @@ -86,6 +91,10 @@
};
}
private formatError(error: Error | any): string {
if (SavedObjectsErrorHelpers.isNotFoundError(error)) {
return WORKSPACE_NOT_FOUND_ERROR;

Check warning on line 95 in src/plugins/workspace/server/workspace_client.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/server/workspace_client.ts#L95

Added line #L95 was not covered by tests
}

return error.message || error.error || 'Error';
}
public async setup(core: CoreSetup): Promise<IResponse<boolean>> {
Expand Down
Loading