-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ingest Manager] Create default Error handler. Use on /setup & EPM ro…
…utes (#74409) * Add default Error handler. Try on /*setup & /epm/* * Add return type. Rename interface * Export error handler type & add comments * use default error handler in installPackageHandler * (re)-add comment
- Loading branch information
John Schulz
committed
Sep 4, 2020
1 parent
e8c41b3
commit ed0f4b9
Showing
4 changed files
with
272 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import Boom from 'boom'; | ||
import { httpServerMock } from 'src/core/server/mocks'; | ||
import { createAppContextStartContractMock } from './mocks'; | ||
|
||
import { | ||
IngestManagerError, | ||
RegistryError, | ||
PackageNotFoundError, | ||
defaultIngestErrorHandler, | ||
} from './errors'; | ||
import { appContextService } from './services'; | ||
|
||
describe('defaultIngestErrorHandler', () => { | ||
let mockContract: ReturnType<typeof createAppContextStartContractMock>; | ||
beforeEach(async () => { | ||
// prevents `Logger not set.` and other appContext errors | ||
mockContract = createAppContextStartContractMock(); | ||
appContextService.start(mockContract); | ||
}); | ||
|
||
afterEach(async () => { | ||
jest.clearAllMocks(); | ||
appContextService.stop(); | ||
}); | ||
|
||
describe('IngestManagerError', () => { | ||
it('502: RegistryError', async () => { | ||
const error = new RegistryError('xyz'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 502, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith(error.message); | ||
}); | ||
|
||
it('404: PackageNotFoundError', async () => { | ||
const error = new PackageNotFoundError('123'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 404, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith(error.message); | ||
}); | ||
|
||
it('400: IngestManagerError', async () => { | ||
const error = new IngestManagerError('123'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 400, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith(error.message); | ||
}); | ||
}); | ||
|
||
describe('Boom', () => { | ||
it('500: constructor - one arg', async () => { | ||
const error = new Boom('bam'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 500, | ||
body: { message: 'An internal server error occurred' }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith('An internal server error occurred'); | ||
}); | ||
|
||
it('custom: constructor - 2 args', async () => { | ||
const error = new Boom('Problem doing something', { | ||
statusCode: 456, | ||
}); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 456, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith('Problem doing something'); | ||
}); | ||
|
||
it('400: Boom.badRequest', async () => { | ||
const error = Boom.badRequest('nope'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 400, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith('nope'); | ||
}); | ||
|
||
it('404: Boom.notFound', async () => { | ||
const error = Boom.notFound('sorry'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 404, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith('sorry'); | ||
}); | ||
}); | ||
|
||
describe('all other errors', () => { | ||
it('500', async () => { | ||
const error = new Error('something'); | ||
const response = httpServerMock.createResponseFactory(); | ||
|
||
await defaultIngestErrorHandler({ error, response }); | ||
|
||
// response | ||
expect(response.ok).toHaveBeenCalledTimes(0); | ||
expect(response.customError).toHaveBeenCalledTimes(1); | ||
expect(response.customError).toHaveBeenCalledWith({ | ||
statusCode: 500, | ||
body: { message: error.message }, | ||
}); | ||
|
||
// logging | ||
expect(mockContract.logger?.error).toHaveBeenCalledTimes(1); | ||
expect(mockContract.logger?.error).toHaveBeenCalledWith(error); | ||
}); | ||
}); | ||
}); |
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.