mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
Fix hapi upgrade (#25723)
* Fixes consistent error handling in routes, adds tests for error handling * Upgrades to failure tests per review
This commit is contained in:
parent
c21cf21e89
commit
f926df8906
4 changed files with 81 additions and 3 deletions
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* 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 { Server } from 'hapi';
|
||||
// @ts-ignore
|
||||
import { initErrorsApi } from '../errors';
|
||||
import { initServicesApi } from '../services';
|
||||
// @ts-ignore
|
||||
import { initStatusApi } from '../status_check';
|
||||
import { initTracesApi } from '../traces';
|
||||
import { initTransactionsApi } from '../transactions';
|
||||
|
||||
describe('route handlers fail properly', () => {
|
||||
let consoleErrorSpy: any;
|
||||
|
||||
async function testRouteFailures(init: (server: Server) => void) {
|
||||
const mockServer = { route: jest.fn() };
|
||||
init((mockServer as unknown) as Server);
|
||||
expect(mockServer.route).toHaveBeenCalled();
|
||||
|
||||
const routes = mockServer.route.mock.calls;
|
||||
const mockReq = {
|
||||
params: {},
|
||||
query: {},
|
||||
pre: {
|
||||
setup: {
|
||||
config: { get: jest.fn() },
|
||||
client: jest.fn(() => Promise.reject(new Error('request failed')))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
routes.forEach(async (route, i) => {
|
||||
test(`route ${i + 1} of ${
|
||||
routes.length
|
||||
} should fail with a Boom error`, async () => {
|
||||
await expect(route[0].handler(mockReq)).rejects.toMatchObject({
|
||||
message: 'request failed',
|
||||
isBoom: true
|
||||
});
|
||||
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
consoleErrorSpy = jest
|
||||
.spyOn(global.console, 'error')
|
||||
.mockImplementation(undefined);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleErrorSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe('error routes', async () => {
|
||||
await testRouteFailures(initErrorsApi);
|
||||
});
|
||||
|
||||
describe('service routes', async () => {
|
||||
await testRouteFailures(initServicesApi);
|
||||
});
|
||||
|
||||
describe('status check routes', async () => {
|
||||
await testRouteFailures(initStatusApi);
|
||||
});
|
||||
|
||||
describe('trace routes', async () => {
|
||||
await testRouteFailures(initTracesApi);
|
||||
});
|
||||
|
||||
describe('transaction routes', async () => {
|
||||
await testRouteFailures(initTransactionsApi);
|
||||
});
|
||||
});
|
|
@ -17,7 +17,7 @@ const defaultErrorHandler = (err: Error) => {
|
|||
// tslint:disable-next-line
|
||||
console.error(err.stack);
|
||||
// @ts-ignore
|
||||
return Boom.boomify(err, { statusCode: 400 });
|
||||
throw Boom.boomify(err, { statusCode: 400 });
|
||||
};
|
||||
|
||||
export function initServicesApi(server: Server) {
|
||||
|
|
|
@ -17,7 +17,7 @@ const defaultErrorHandler = (err: Error) => {
|
|||
// tslint:disable-next-line
|
||||
console.error(err.stack);
|
||||
// @ts-ignore
|
||||
return Boom.boomify(err, { statusCode: 400 });
|
||||
throw Boom.boomify(err, { statusCode: 400 });
|
||||
};
|
||||
|
||||
export function initTracesApi(server: Server) {
|
||||
|
|
|
@ -22,7 +22,7 @@ const defaultErrorHandler = (err: Error) => {
|
|||
// tslint:disable-next-line
|
||||
console.error(err.stack);
|
||||
// @ts-ignore
|
||||
Boom.boomify(err, { statusCode: err.statusCode || 400 });
|
||||
throw Boom.boomify(err, { statusCode: 400 });
|
||||
};
|
||||
|
||||
export function initTransactionsApi(server: Server) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue