-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
return 404 instead of 500 for missing agent config name
Signed-off-by: Yulong Ruan <[email protected]>
- Loading branch information
Showing
11 changed files
with
223 additions
and
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { handleError } from './error_handler'; | ||
import { loggerMock } from '../../../../src/core/server/logging/logger.mock'; | ||
import { AgentNotFoundError } from './errors'; | ||
import { opensearchDashboardsResponseFactory } from '../../../../src/core/server'; | ||
|
||
describe('Error handler', () => { | ||
it('should return 404 not found response if error is AgentNotFoundError', () => { | ||
const mockedLogger = loggerMock.create(); | ||
const error = handleError( | ||
new AgentNotFoundError('test error'), | ||
opensearchDashboardsResponseFactory, | ||
mockedLogger | ||
); | ||
expect(error.status).toBe(404); | ||
expect(error.options.body).toMatchInlineSnapshot('"Agent not found"'); | ||
}); | ||
|
||
it('should return 4xx with original error body', () => { | ||
const mockedLogger = loggerMock.create(); | ||
const error = handleError( | ||
{ | ||
statusCode: 429, | ||
body: { | ||
status: 429, | ||
error: { | ||
type: 'OpenSearchStatusException', | ||
reason: 'System Error', | ||
details: 'Request is throttled at model level.', | ||
}, | ||
}, | ||
}, | ||
opensearchDashboardsResponseFactory, | ||
mockedLogger | ||
); | ||
expect(error.status).toBe(429); | ||
expect(error.options.body).toMatchInlineSnapshot(` | ||
Object { | ||
"message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", | ||
} | ||
`); | ||
}); | ||
|
||
it('shuld return generic 5xx error', () => { | ||
const mockedLogger = loggerMock.create(); | ||
const error = handleError( | ||
{ | ||
statusCode: 502, | ||
body: { | ||
status: 502, | ||
error: { | ||
type: 'OpenSearchStatusException', | ||
reason: 'System Error', | ||
details: 'Some bad thing happened', | ||
}, | ||
}, | ||
}, | ||
opensearchDashboardsResponseFactory, | ||
mockedLogger | ||
); | ||
expect(error.status).toBe(502); | ||
|
||
// No extra info should returned | ||
expect(error.payload).toBe(undefined); | ||
expect(error.options.body).toBe(undefined); | ||
}); | ||
|
||
it('should return generic internalError for unhandled server-side issues', () => { | ||
const mockedLogger = loggerMock.create(); | ||
const error = handleError( | ||
new Error('Arbitrary Error'), | ||
opensearchDashboardsResponseFactory, | ||
mockedLogger | ||
); | ||
expect(error.status).toBe(500); | ||
expect(error.payload).toEqual('Internal Error'); | ||
expect(error.options).toMatchInlineSnapshot('Object {}'); | ||
}); | ||
}); |
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,33 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Logger, OpenSearchDashboardsResponseFactory } from '../../../../src/core/server'; | ||
import { AgentNotFoundError } from './errors'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
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) { | ||
return res.customError({ | ||
body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, | ||
statusCode: e.statusCode, | ||
}); | ||
} else { | ||
return res.customError({ | ||
statusCode: e.statusCode, | ||
}); | ||
} | ||
} | ||
|
||
// Return an general internalError for unhandled server-side issues | ||
return res.internalError(); | ||
}; |
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,11 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export class AgentNotFoundError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.message = 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
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
Oops, something went wrong.