From cd2afab043f2e8f931f3a6d9250b3ed427b695b9 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Tue, 3 Dec 2019 14:24:03 +0100 Subject: [PATCH] switch to boom wrapper --- src/plugins/share/server/routes/goto.ts | 45 ++++++-------- .../server/routes/lib/short_url_error.test.ts | 62 ------------------- .../server/routes/lib/short_url_error.ts | 28 --------- .../share/server/routes/shorten_url.ts | 20 +++--- 4 files changed, 28 insertions(+), 127 deletions(-) delete mode 100644 src/plugins/share/server/routes/lib/short_url_error.test.ts delete mode 100644 src/plugins/share/server/routes/lib/short_url_error.ts diff --git a/src/plugins/share/server/routes/goto.ts b/src/plugins/share/server/routes/goto.ts index a8f9d887a064e..454beddd19c92 100644 --- a/src/plugins/share/server/routes/goto.ts +++ b/src/plugins/share/server/routes/goto.ts @@ -20,7 +20,6 @@ import { CoreSetup, IRouter } from 'kibana/server'; import { schema } from '@kbn/config-schema'; -import { handleShortUrlError } from './lib/short_url_error'; import { shortUrlAssertValid } from './lib/short_url_assert_valid'; import { ShortUrlLookupService } from './lib/short_url_lookup'; @@ -40,31 +39,27 @@ export const createGotoRoute = ({ params: schema.object({ urlId: schema.string() }), }, }, - async function(context, request, response) { - try { - const url = await shortUrlLookup.getUrl(request.params.urlId, { - savedObjects: context.core.savedObjects.client, - }); - shortUrlAssertValid(url); + router.handleLegacyErrors(async function(context, request, response) { + const url = await shortUrlLookup.getUrl(request.params.urlId, { + savedObjects: context.core.savedObjects.client, + }); + shortUrlAssertValid(url); - const uiSettings = context.core.uiSettings.client; - const stateStoreInSessionStorage = await uiSettings.get('state:storeInSessionStorage'); - if (!stateStoreInSessionStorage) { - return response.redirected({ - headers: { - location: http.basePath.prepend(url), - }, - }); - } else { - return response.redirected({ - headers: { - location: http.basePath.prepend('/goto_LP/' + request.params.urlId), - }, - }); - } - } catch (err) { - return handleShortUrlError(response, err); + const uiSettings = context.core.uiSettings.client; + const stateStoreInSessionStorage = await uiSettings.get('state:storeInSessionStorage'); + if (!stateStoreInSessionStorage) { + return response.redirected({ + headers: { + location: http.basePath.prepend(url), + }, + }); + } else { + return response.redirected({ + headers: { + location: http.basePath.prepend('/goto_LP/' + request.params.urlId), + }, + }); } - } + }) ); }; 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 deleted file mode 100644 index dabcfb18d5882..0000000000000 --- a/src/plugins/share/server/routes/lib/short_url_error.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -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), - createErrorWithStatusCode(403), - createErrorWithStatusCode(404), - ]; - - const uncaughtErrors = [new Error(), createErrorWithStatusCode(500)]; - - caughtErrorsWithStatusCode.forEach(err => { - const statusCode = (err as Boom).output.statusCode; - const response = createResponseStub(); - it(`should handle errors with statusCode of ${statusCode}`, function() { - handleShortUrlError(response, err); - expect(response.customError).toHaveBeenCalledWith(expect.objectContaining({ statusCode })); - }); - }); - - uncaughtErrors.forEach(err => { - it(`should not handle unknown errors`, function() { - 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 deleted file mode 100644 index feb916fe80199..0000000000000 --- a/src/plugins/share/server/routes/lib/short_url_error.ts +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import Boom from 'boom'; -import { KibanaResponseFactory } from 'kibana/server'; - -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 fe206179b4e19..1acb187c932b2 100644 --- a/src/plugins/share/server/routes/shorten_url.ts +++ b/src/plugins/share/server/routes/shorten_url.ts @@ -20,7 +20,6 @@ import { IRouter } from 'kibana/server'; import { schema } from '@kbn/config-schema'; -import { handleShortUrlError } from './lib/short_url_error'; import { shortUrlAssertValid } from './lib/short_url_assert_valid'; import { ShortUrlLookupService } from './lib/short_url_lookup'; @@ -38,16 +37,13 @@ export const createShortenUrlRoute = ({ body: schema.object({ url: schema.string() }, { allowUnknowns: false }), }, }, - async function(context, request, response) { - try { - shortUrlAssertValid(request.body.url); - const urlId = await shortUrlLookup.generateUrlId(request.body.url, { - savedObjects: context.core.savedObjects.client, - }); - return response.ok({ body: { urlId } }); - } catch (err) { - return handleShortUrlError(response, err); - } - } + router.handleLegacyErrors(async function(context, request, response) { + shortUrlAssertValid(request.body.url); + const urlId = await shortUrlLookup.generateUrlId(request.body.url, { + savedObjects: context.core.savedObjects.client, + }); + throw new Error(); + return response.ok({ body: { urlId } }); + }) ); };