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

500 error if workspace is invalid #429

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

hutiechuan
Copy link
Contributor

Description

[Describe what this change achieves]

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test.
  • New functionality has user manual doc added.
  • Commits are signed per the DCO using --signoff.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Comment on lines 107 to 118
} catch (err) {
console.error('Error creating OpenSearch client:', err);

if (
err.statusCode === 403 &&
err.message?.includes('Saved object does not belong to the workspace')
) {
return res.notFound({
body: 'Workspace/data source is invalid or not found!!',
});
}
if (
err.message?.includes('Data source not found') ||
err.message?.includes('Workspace not found') ||
err.message?.includes('Saved object does not belong to the workspace')
) {
return res.notFound({
body: 'Workspace/data source is invalid or not found!!',
});
}

return handleError(err, res, context.assistant_plugin.logger);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

should the whole error hanling be inside of getOpenSearchClientTransport method? It's been shared by other apis as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we could also do that.


if (
err.statusCode === 403 &&
err.message?.includes('Saved object does not belong to the workspace')
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we have a better way instead of reply on message content?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

Comment on lines 119 to 121
err.message?.includes('Data source not found') ||
err.message?.includes('Workspace not found') ||
err.message?.includes('Saved object does not belong to the workspace')
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it

err.message?.includes('Saved object does not belong to the workspace')
) {
return res.notFound({
body: 'Workspace/data source is invalid or not found!!',
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you add i18n support for the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

Copy link
Member

Choose a reason for hiding this comment

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

Do we need i18n for server side error message?

@hutiechuan hutiechuan force-pushed the 500Error branch 2 times, most recently from 97139f9 to 3999e6f Compare February 6, 2025 09:28
try {
return (await context.dataSource.opensearch.getClient(dataSourceId)).transport;
} catch (err) {
throw new DataSourceNotFoundError('Saved object does not belong to the workspace');
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we check the error to decide whether to convert to DataSourceNotFoundError or not? What's the original err thrown if dataSourceId is not valid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, error is: DataSourceError: Data Source Error: Saved object [data-source/3115e810-e39b-11ef-bf81-bf8337ab04fe] not found

Copy link
Member

@ruanyl ruanyl Feb 8, 2025

Choose a reason for hiding this comment

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

@hutiechuan I think the point is we are not sure if the err here is about data source not found, it could be any unhandled exception, for example, network issue. So re-throw any error as DataSourceNotFoundError is inappropriate.
I'd suggest to not try/catch here, instead let handleError function to handle the error

export const handleError = (e: any, res: OpenSearchDashboardsResponseFactory, logger: Logger) => {
  logger.error('Error occurred', e);
  // Handle specific type of Errors
  if (e instanceof AgentNotFoundError) {
    return res.notFound({ body: 'Agent not found' });
  }

  // handle http response error of calling backend API
  if (e.statusCode) {
    if (e.statusCode >= 400 && e.statusCode <= 499) {
+      let message = typeof e.body === 'string' ? e.body : JSON.stringify(e.body);
+      if (!message) {
+        message = e.message;
+      }
      return res.customError({
+        body: { message: message || 'unknow error' },
        statusCode: e.statusCode,
      });
    } else {
      return res.customError({
        statusCode: e.statusCode,
      });
    }
  }

  // Return an general internalError for unhandled server-side issues
  return res.internalError();
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but when i console the original err, it was throwing the message of "DataSourceError: Data Source Error: Saved object [data-source/3115e810-e39b-11ef-bf81-bf8337ab04fe] not found"

so instead of doing that, you don't want me to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and if i dont try to catch the error of DataSourceNotFoundError, it wouldn't go to this condition if (e.statusCode >= 400 && e.statusCode <= 499) since the statusCode is 500

Copy link
Member

Choose a reason for hiding this comment

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

I saw statusCode is 403, did you try?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, i tried.
But on my side, the original error is 404

Original error: DataSourceError: Data Source Error: Saved object [data-source/3115e810-e39b-11ef-bf81-bf8337ab04fe] not found
at createDataSourceError (/Users/hutiech/Desktop/OpenSearch-Dashboards/src/plugins/data_source/server/lib/error.ts:13:12)
at configureClient (/Users/hutiech/Desktop/OpenSearch-Dashboards/src/plugins/data_source/server/client/configure_client.ts:105:32)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at getOpenSearchClientTransport (/Users/hutiech/Desktop/OpenSearch-Dashboards/plugins/dashboards-assistant/server/utils/get_opensearch_client_transport.ts:32:15)
at /Users/hutiech/Desktop/OpenSearch-Dashboards/plugins/dashboards-assistant/server/routes/summary_routes.ts:105:24
at /Users/hutiech/Desktop/OpenSearch-Dashboards/src/core/server/http/router/error_wrapper.ts:37:14
at Router.handle (/Users/hutiech/Desktop/OpenSearch-Dashboards/src/core/server/http/router/router.ts:286:44)
at handler (/Users/hutiech/Desktop/OpenSearch-Dashboards/src/core/server/http/router/router.ts:241:11)
at exports.Manager.execute (/Users/hutiech/Desktop/OpenSearch-Dashboards/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
at Object.internals.handler (/Users/hutiech/Desktop/OpenSearch-Dashboards/node_modules/@hapi/hapi/lib/handler.js:46:20)
at exports.execute (/Users/hutiech/Desktop/OpenSearch-Dashboards/node_modules/@hapi/hapi/lib/handler.js:31:20)
at Request._lifecycle (/Users/hutiech/Desktop/OpenSearch-Dashboards/node_modules/@hapi/hapi/lib/request.js:371:32)
at Request._execute (/Users/hutiech/Desktop/OpenSearch-Dashboards/node_modules/@hapi/hapi/lib/request.js:281:9) {
statusCode: 404,
body: undefined
}

Copy link
Member

Choose a reason for hiding this comment

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

so if it's 404, it will be handled by if (e.statusCode >= 400 && e.statusCode <= 499)

Comment on lines 104 to 118
let client;
try {
client = await getOpenSearchClientTransport({
context,
dataSourceId: req.query.dataSourceId,
});
} catch (err) {
if (err instanceof DataSourceNotFoundError) {
const msg = i18n.translate('assistant.server.error.workspaceDataSourceNotFound', {
defaultMessage: 'Workspace/data source is invalid or not found.',
});
return res.notFound({ body: msg });
}
return handleError(err, res, context.assistant_plugin.logger);
}
Copy link
Member

Choose a reason for hiding this comment

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

@hutiechuan there is a try/catch statement below, we can move const client = await getOpenSearchClientTransport(...) to there, so we can handle the error in a centralized place handleError()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yessir

yarn.lock Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Could revert these changes? seems unrelated to this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which changes

Copy link
Member

Choose a reason for hiding this comment

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

The changes of this file (yarn.lock).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it

Comment on lines 19 to 22
// Handle specific type of Errors
if (e instanceof AgentNotFoundError) {
return res.notFound({ body: 'Agent not found' });
}
Copy link
Member

Choose a reason for hiding this comment

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

This is duplicated, let's remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

yarn.lock Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated file changes, please revert it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link

codecov bot commented Feb 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.00%. Comparing base (581a7ca) to head (151b08b).
Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #429      +/-   ##
==========================================
- Coverage   87.46%   85.00%   -2.47%     
==========================================
  Files          65       72       +7     
  Lines        1891     2107     +216     
  Branches      473      520      +47     
==========================================
+ Hits         1654     1791     +137     
- Misses        236      314      +78     
- Partials        1        2       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

import { Logger, OpenSearchDashboardsResponseFactory } from '../../../../src/core/server';
import { AgentNotFoundError } from './errors';
// import { DataSourceNotFoundError } from '../utils/get_opensearch_client_transport';
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove the commented code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@@ -12,6 +13,8 @@ import { AssistantServiceSetup } from '../services/assistant_service';
import { handleError } from './error_handler';
import { AgentNotFoundError } from './errors';

import { DataSourceNotFoundError } from '../utils/get_opensearch_client_transport';
Copy link
Member

Choose a reason for hiding this comment

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

This import never used, we can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

Comment on lines 8 to 20
interface ErrorWithStatusCode extends Error {
statusCode?: number;
}

export class DataSourceNotFoundError extends Error {
public readonly statusCode: number;
constructor(message: string, originalError?: ErrorWithStatusCode) {
super(message);
this.name = 'DataSourceNotFoundError';
this.statusCode = originalError?.statusCode || 403;
}
}

Copy link
Member

Choose a reason for hiding this comment

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

These changes are not going to be needed, we can remove them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

@@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { i18n } from '@osd/i18n';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import { i18n } from '@osd/i18n';

@ruanyl ruanyl added the backport 2.x Trigger the backport flow to 2.x label Feb 11, 2025
@ruanyl
Copy link
Member

ruanyl commented Feb 12, 2025

@hutiechuan there are several checks failed, please add changelog and fix the DCO.

@hutiechuan hutiechuan force-pushed the 500Error branch 4 times, most recently from 217c847 to 7ea5e96 Compare February 12, 2025 08:57
hutiechuan added 10 commits February 12, 2025 17:04
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
@hutiechuan
Copy link
Contributor Author

@hutiechuan there are several checks failed, please add changelog and fix the DCO.

I added the changelog, and fix the DCO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Trigger the backport flow to 2.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants