From 97d1679cf18877da55fe0e425b238b067605199b Mon Sep 17 00:00:00 2001 From: AF <4775679+datalek@users.noreply.github.com> Date: Fri, 22 Mar 2024 10:28:55 +0100 Subject: [PATCH] [DEV-1545] Add custom PathReporter (#770) --- .changeset/polite-buses-agree.md | 5 +++++ apps/nextjs-website/src/lib/strapi/PathReporter.ts | 14 ++++++++++++++ .../lib/strapi/__tests__/fetchFromStrapi.test.ts | 2 +- .../src/lib/strapi/fetchFromStrapi.ts | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changeset/polite-buses-agree.md create mode 100644 apps/nextjs-website/src/lib/strapi/PathReporter.ts diff --git a/.changeset/polite-buses-agree.md b/.changeset/polite-buses-agree.md new file mode 100644 index 0000000000..1a21837c58 --- /dev/null +++ b/.changeset/polite-buses-agree.md @@ -0,0 +1,5 @@ +--- +"nextjs-website": patch +--- + +[DEV-1545] Add custom PathReporter to improve readability of errors raised by decode diff --git a/apps/nextjs-website/src/lib/strapi/PathReporter.ts b/apps/nextjs-website/src/lib/strapi/PathReporter.ts new file mode 100644 index 0000000000..dee7ede82f --- /dev/null +++ b/apps/nextjs-website/src/lib/strapi/PathReporter.ts @@ -0,0 +1,14 @@ +import * as t from 'io-ts'; + +const getMessage = (error: t.ValidationError): string => { + if (error.message) { + return error.message; + } else { + const jsonPath = error.context.map(({ key }) => `${key}`).join('/'); + const expected = error.context[error.context.length - 1].type.name; + return `Invalid value ${error.value} supplied to '${jsonPath}', expected type ${expected}`; + } +}; + +export const failure = (errors: t.Errors): ReadonlyArray => + errors.map(getMessage); diff --git a/apps/nextjs-website/src/lib/strapi/__tests__/fetchFromStrapi.test.ts b/apps/nextjs-website/src/lib/strapi/__tests__/fetchFromStrapi.test.ts index d8961140e4..2d40c0aa86 100644 --- a/apps/nextjs-website/src/lib/strapi/__tests__/fetchFromStrapi.test.ts +++ b/apps/nextjs-website/src/lib/strapi/__tests__/fetchFromStrapi.test.ts @@ -109,7 +109,7 @@ describe('fetchFromStrapi', () => { }); const actual = fetchFromStrapi('aPath', 'aPopulate', badCodec)(env); const expected = new Error( - 'Invalid value 1 supplied to : {| data: {| id: string |} |}/data: {| id: string |}/id: string' + `Invalid value 1 supplied to '/data/id', expected type string` ); await expect(actual).rejects.toStrictEqual(expected); }); diff --git a/apps/nextjs-website/src/lib/strapi/fetchFromStrapi.ts b/apps/nextjs-website/src/lib/strapi/fetchFromStrapi.ts index 4acd56cfb9..9c16a0b459 100644 --- a/apps/nextjs-website/src/lib/strapi/fetchFromStrapi.ts +++ b/apps/nextjs-website/src/lib/strapi/fetchFromStrapi.ts @@ -3,7 +3,7 @@ import { pipe } from 'fp-ts/lib/function'; import * as R from 'fp-ts/lib/Reader'; import * as E from 'fp-ts/lib/Either'; import * as TE from 'fp-ts/lib/TaskEither'; -import * as PR from 'io-ts/lib/PathReporter'; +import * as PR from './PathReporter'; import { StrapiEnv } from '@/lib/strapi/StapiEnv'; // Function to invoke in order to retrieve data from Strapi.