Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Remove try/catch for short url so the appropriate errors will be prop… #13179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions src/server/http/__tests__/short_url_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,49 @@ import expect from 'expect.js';
import _ from 'lodash';
import { handleShortUrlError } from '../short_url_error';

function createErrorWithStatus(status) {
const error = new Error();
error.status = status;
return error;
}

function createErrorWithStatusCode(statusCode) {
const error = new Error();
error.statusCode = statusCode;
return error;
}

describe('handleShortUrlError()', () => {
const caughtErrors = [{
status: 401
}, {
status: 403
}, {
status: 404
}];

const uncaughtErrors = [{
status: null
}, {
status: 500
}];

caughtErrors.forEach((err) => {
it(`should handle ${err.status} errors`, function () {
const caughtErrorsWithStatus = [
createErrorWithStatus(401),
createErrorWithStatus(403),
createErrorWithStatus(404),
];

const caughtErrorsWithStatusCode = [
createErrorWithStatusCode(401),
createErrorWithStatusCode(403),
createErrorWithStatusCode(404),
];

const uncaughtErrors = [
new Error(),
createErrorWithStatus(500),
createErrorWithStatusCode(500)
];

caughtErrorsWithStatus.forEach((err) => {
it(`should handle errors with status of ${err.status}`, function () {
expect(_.get(handleShortUrlError(err), 'output.statusCode')).to.be(err.status);
});
});

caughtErrorsWithStatusCode.forEach((err) => {
it(`should handle errors with statusCode of ${err.statusCode}`, function () {
expect(_.get(handleShortUrlError(err), 'output.statusCode')).to.be(err.statusCode);
});
});

uncaughtErrors.forEach((err) => {
it(`should not handle unknown errors`, function () {
expect(_.get(handleShortUrlError(err), 'output.statusCode')).to.be(500);
Expand Down
8 changes: 2 additions & 6 deletions src/server/http/short_url_error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Boom from 'boom';

export function handleShortUrlError(err) {
if (err.isBoom) return err;
if (err.status === 401) return Boom.unauthorized();
if (err.status === 403) return Boom.forbidden();
if (err.status === 404) return Boom.notFound();
return Boom.badImplementation();
export function handleShortUrlError(error) {
return Boom.wrap(error, error.statusCode || error.status || 500);
}
10 changes: 3 additions & 7 deletions src/server/http/short_url_lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ export default function (server) {
},

async getUrl(id, req) {
try {
const doc = await req.getSavedObjectsClient().get('url', id);
updateMetadata(doc, req);
const doc = await req.getSavedObjectsClient().get('url', id);
updateMetadata(doc, req);

return doc.attributes.url;
} catch (err) {
return '/';
}
return doc.attributes.url;
}
};
}