-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: main
Are you sure you want to change the base?
Conversation
c25bec8
to
37eda3e
Compare
server/routes/summary_routes.ts
Outdated
} 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); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
server/routes/summary_routes.ts
Outdated
|
||
if ( | ||
err.statusCode === 403 && | ||
err.message?.includes('Saved object does not belong to the workspace') |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
server/routes/summary_routes.ts
Outdated
err.message?.includes('Data source not found') || | ||
err.message?.includes('Workspace not found') || | ||
err.message?.includes('Saved object does not belong to the workspace') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it
server/routes/summary_routes.ts
Outdated
err.message?.includes('Saved object does not belong to the workspace') | ||
) { | ||
return res.notFound({ | ||
body: 'Workspace/data source is invalid or not found!!', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
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?
97139f9
to
3999e6f
Compare
try { | ||
return (await context.dataSource.opensearch.getClient(dataSourceId)).transport; | ||
} catch (err) { | ||
throw new DataSourceNotFoundError('Saved object does not belong to the workspace'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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();
};
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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)
server/routes/summary_routes.ts
Outdated
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); | ||
} |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yessir
yarn.lock
Outdated
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which changes
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it
server/routes/error_handler.ts
Outdated
// Handle specific type of Errors | ||
if (e instanceof AgentNotFoundError) { | ||
return res.notFound({ body: 'Agent not found' }); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
yarn.lock
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
server/routes/error_handler.ts
Outdated
import { Logger, OpenSearchDashboardsResponseFactory } from '../../../../src/core/server'; | ||
import { AgentNotFoundError } from './errors'; | ||
// import { DataSourceNotFoundError } from '../utils/get_opensearch_client_transport'; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
server/routes/summary_routes.ts
Outdated
@@ -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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
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; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { i18n } from '@osd/i18n'; |
@hutiechuan there are several checks failed, please add changelog and fix the DCO. |
217c847
to
7ea5e96
Compare
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: Tianyu Gao <[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]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
Signed-off-by: hutiechuan <[email protected]>
I added the changelog, and fix the DCO |
Description
[Describe what this change achieves]
Issues Resolved
[List any issues this PR will resolve]
Check List
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.