Skip to content

Commit

Permalink
fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Dec 3, 2019
1 parent bbc441c commit 44c4c9c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/plugins/share/server/routes/goto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const createGotoRoute = ({
});
}
} catch (err) {
throw handleShortUrlError(err);
return handleShortUrlError(response, err);
}
}
);
Expand Down
17 changes: 15 additions & 2 deletions src/plugins/share/server/routes/lib/short_url_error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<KibanaResponseFactory>;
}

describe('handleShortUrlError()', () => {
const caughtErrorsWithStatusCode = [
createErrorWithStatusCode(401),
Expand All @@ -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 })
);
});
});
});
6 changes: 4 additions & 2 deletions src/plugins/share/server/routes/lib/short_url_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
2 changes: 1 addition & 1 deletion src/plugins/share/server/routes/shorten_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const createShortenUrlRoute = ({
});
return response.ok({ body: { urlId } });
} catch (err) {
throw handleShortUrlError(err);
return handleShortUrlError(response, err);
}
}
);
Expand Down

0 comments on commit 44c4c9c

Please sign in to comment.