From 44c4c9c7e77bf1aef031e6e5353bc2d41768dea8 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Tue, 3 Dec 2019 14:10:46 +0100 Subject: [PATCH] fix error handling --- src/plugins/share/server/routes/goto.ts | 2 +- .../server/routes/lib/short_url_error.test.ts | 17 +++++++++++++++-- .../share/server/routes/lib/short_url_error.ts | 6 ++++-- src/plugins/share/server/routes/shorten_url.ts | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/plugins/share/server/routes/goto.ts b/src/plugins/share/server/routes/goto.ts index 1aae0b830f25a..a8f9d887a064e 100644 --- a/src/plugins/share/server/routes/goto.ts +++ b/src/plugins/share/server/routes/goto.ts @@ -63,7 +63,7 @@ export const createGotoRoute = ({ }); } } catch (err) { - throw handleShortUrlError(err); + return handleShortUrlError(response, err); } } ); diff --git a/src/plugins/share/server/routes/lib/short_url_error.test.ts b/src/plugins/share/server/routes/lib/short_url_error.test.ts index d0176db94f193..dabcfb18d5882 100644 --- a/src/plugins/share/server/routes/lib/short_url_error.test.ts +++ b/src/plugins/share/server/routes/lib/short_url_error.test.ts @@ -20,11 +20,18 @@ import _ from 'lodash'; import { handleShortUrlError } from './short_url_error'; import Boom from 'boom'; +import { KibanaResponseFactory } from 'kibana/server'; function createErrorWithStatusCode(statusCode: number) { return new Boom('', { statusCode }); } +function createResponseStub() { + return ({ + customError: jest.fn(), + } as unknown) as jest.Mocked; +} + describe('handleShortUrlError()', () => { const caughtErrorsWithStatusCode = [ createErrorWithStatusCode(401), @@ -36,14 +43,20 @@ describe('handleShortUrlError()', () => { caughtErrorsWithStatusCode.forEach(err => { const statusCode = (err as Boom).output.statusCode; + const response = createResponseStub(); it(`should handle errors with statusCode of ${statusCode}`, function() { - expect(_.get(handleShortUrlError(err), 'output.statusCode')).toBe(statusCode); + handleShortUrlError(response, err); + expect(response.customError).toHaveBeenCalledWith(expect.objectContaining({ statusCode })); }); }); uncaughtErrors.forEach(err => { it(`should not handle unknown errors`, function() { - expect(_.get(handleShortUrlError(err), 'output.statusCode')).toBe(500); + const response = createResponseStub(); + handleShortUrlError(response, err); + expect(response.customError).toHaveBeenCalledWith( + expect.objectContaining({ statusCode: 500 }) + ); }); }); }); diff --git a/src/plugins/share/server/routes/lib/short_url_error.ts b/src/plugins/share/server/routes/lib/short_url_error.ts index 3730bea65a4bc..feb916fe80199 100644 --- a/src/plugins/share/server/routes/lib/short_url_error.ts +++ b/src/plugins/share/server/routes/lib/short_url_error.ts @@ -18,9 +18,11 @@ */ import Boom from 'boom'; +import { KibanaResponseFactory } from 'kibana/server'; -export function handleShortUrlError(error: Error) { - return Boom.boomify(error, { +export function handleShortUrlError(response: KibanaResponseFactory, error: Error) { + return response.customError({ statusCode: Boom.isBoom(error) ? error.output.statusCode : 500, + body: error.message, }); } diff --git a/src/plugins/share/server/routes/shorten_url.ts b/src/plugins/share/server/routes/shorten_url.ts index 4f72e10d4d3d2..fe206179b4e19 100644 --- a/src/plugins/share/server/routes/shorten_url.ts +++ b/src/plugins/share/server/routes/shorten_url.ts @@ -46,7 +46,7 @@ export const createShortenUrlRoute = ({ }); return response.ok({ body: { urlId } }); } catch (err) { - throw handleShortUrlError(err); + return handleShortUrlError(response, err); } } );