From a0991c581bcc4d5b903369e24e798e74995adbae Mon Sep 17 00:00:00 2001 From: slorber Date: Wed, 3 Jun 2020 16:07:27 +0200 Subject: [PATCH] add context to error message --- .../redirectValidation.test.ts.snap | 16 ++++++++++++++++ .../src/__tests__/redirectValidation.test.ts | 12 +++--------- .../src/redirectValidation.ts | 7 ++++++- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap b/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap new file mode 100644 index 0000000000..5c4ea33911 --- /dev/null +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap @@ -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\\"}" +`; diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts index 5d141843ec..035001e40e 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts @@ -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(); }); }); diff --git a/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts b/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts index 1d04e19d6b..7c732d0375 100644 --- a/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts +++ b/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts @@ -22,5 +22,10 @@ const RedirectSchema = Yup.object({ }); 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)}`); + } }