forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MD]Improve datasource server side error handling (opensearch-project…
…#2236) Signed-off-by: Su <[email protected]>
- Loading branch information
1 parent
d8f592a
commit 88d9a39
Showing
6 changed files
with
106 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
import { DataSourceConfigError } from './error'; | ||
|
||
describe('DataSourceConfigError', () => { | ||
it('create from savedObject bad request error should be 400 error', () => { | ||
const error = SavedObjectsErrorHelpers.createBadRequestError('test reason message'); | ||
expect(new DataSourceConfigError('test prefix: ', error)).toMatchObject({ | ||
statusCode: 400, | ||
message: 'test prefix: test reason message: Bad Request', | ||
}); | ||
}); | ||
|
||
it('create from savedObject not found error should be 400 error', () => { | ||
const error = SavedObjectsErrorHelpers.decorateNotAuthorizedError(new Error()); | ||
expect(new DataSourceConfigError('test prefix: ', error)).toHaveProperty('statusCode', 400); | ||
}); | ||
|
||
it('create from savedObject service unavailable error should be a 500 error', () => { | ||
const error = SavedObjectsErrorHelpers.decorateOpenSearchUnavailableError( | ||
new Error('test reason message') | ||
); | ||
expect(new DataSourceConfigError('test prefix: ', error)).toMatchObject({ | ||
statusCode: 500, | ||
message: 'test prefix: test reason message', | ||
}); | ||
}); | ||
|
||
it('create from non savedObject error should always be a 400 error', () => { | ||
const error = new Error('test reason message'); | ||
expect(new DataSourceConfigError('test prefix: ', error)).toMatchObject({ | ||
statusCode: 400, | ||
message: 'test prefix: test reason message', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; | ||
import { OsdError } from '../../../../../src/plugins/opensearch_dashboards_utils/common'; | ||
|
||
export class DataSourceConfigError extends OsdError { | ||
// must have statusCode to avoid route handler in search.ts to return 500 | ||
statusCode: number; | ||
constructor(messagePrefix: string, error: any) { | ||
const messageContent = SavedObjectsErrorHelpers.isSavedObjectsClientError(error) | ||
? error.output.payload.message | ||
: error.message; | ||
super(messagePrefix + messageContent); | ||
// Cast all 5xx error returned by saveObjectClient to 500, 400 for both savedObject client | ||
// 4xx errors, and other errors | ||
this.statusCode = SavedObjectsErrorHelpers.isOpenSearchUnavailableError(error) ? 500 : 400; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters