add context to error message

This commit is contained in:
slorber 2020-06-03 16:07:27 +02:00
parent 5bf85e44ac
commit a0991c581b
3 changed files with 25 additions and 10 deletions

View file

@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`validateRedirect throw for bad redirects 1`] = `
"fromRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string
Redirect={\\"fromRoutePath\\":\\"https://fb.com/fromSomePath\\",\\"toRoutePath\\":\\"/toSomePath\\"}"
`;
exports[`validateRedirect throw for bad redirects 2`] = `
"toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string
Redirect={\\"fromRoutePath\\":\\"/fromSomePath\\",\\"toRoutePath\\":\\"https://fb.com/toSomePath\\"}"
`;
exports[`validateRedirect throw for bad redirects 3`] = `
"toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string
Redirect={\\"fromRoutePath\\":\\"/fromSomePath\\",\\"toRoutePath\\":\\"/toSomePath?queryString=xyz\\"}"
`;

View file

@ -33,26 +33,20 @@ describe('validateRedirect', () => {
fromRoutePath: 'https://fb.com/fromSomePath',
toRoutePath: '/toSomePath',
}),
).toThrowErrorMatchingInlineSnapshot(
`"fromRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`,
);
).toThrowErrorMatchingSnapshot();
expect(() =>
validateRedirect({
fromRoutePath: '/fromSomePath',
toRoutePath: 'https://fb.com/toSomePath',
}),
).toThrowErrorMatchingInlineSnapshot(
`"toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`,
);
).toThrowErrorMatchingSnapshot();
expect(() =>
validateRedirect({
fromRoutePath: '/fromSomePath',
toRoutePath: '/toSomePath?queryString=xyz',
}),
).toThrowErrorMatchingInlineSnapshot(
`"toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`,
);
).toThrowErrorMatchingSnapshot();
});
});

View file

@ -22,5 +22,10 @@ const RedirectSchema = Yup.object<RedirectMetadata>({
});
export function validateRedirect(redirect: RedirectMetadata) {
RedirectSchema.validateSync(redirect);
try {
RedirectSchema.validateSync(redirect);
} catch (e) {
// Tells the user which redirect is the problem!
throw new Error(`${e.message}\nRedirect=${JSON.stringify(redirect)}`);
}
}