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

Improve error handling for polymorphic field data with missing type value #297

Open
atahanyorganci opened this issue Sep 4, 2022 · 4 comments
Labels

Comments

@atahanyorganci
Copy link

I encountered an error with following code trying to add polymorphic nested objects to a list.

export const Link = defineNestedType(() => ({
    name: "Link",
    description: "Link to an external resource",
    fields: {
        text: {
            type: "string",
            required: true,
        },
        url: {
            type: "string",
            required: true,
        },
    },
}));

export const LinkList = defineNestedType(() => ({
    name: "LinkList",
    description: "List of links to external resources",
    fields: {
        text: {
            type: "string",
            required: true,
        },
        links: {
            type: "list",
            required: true,
            of: Link,
        },
    },
}));

export const NavbarLinks = defineDocumentType(() => ({
    name: "NavbarLinks",
    filePathPattern: "core/navbarLinks.json",
    isSingleton: true,
    fields: {
        links: {
            type: "list",
            of: [Link, LinkList],
            required: true,
        },
    },
}));

This produces following stack trace

TypeError: Cannot read properties of undefined (reading 'replace')
    at idToFileName (file://.../node_modules/@contentlayer/core/src/generation/generate-dotpkg.ts:389:89)
    at makeDataExportFile (file://.../node_modules/@contentlayer/core/src/generation/generate-dotpkg.ts:291:53)
    at file://.../node_modules/@contentlayer/core/src/generation/generate-dotpkg.ts:153:18
    at Array.map (<anonymous>)
    at next (file://.../node_modules/@contentlayer/core/src/generation/generate-dotpkg.ts:152:44)
    at Generator.next (<anonymous>)
    at ApplyFrame.apply (file://.../node_modules/@effect-ts/system/_src/Effect/gen.ts:148:36)
    at FiberContext.nextInstr (file://.../node_modules/@effect-ts/system/_src/Fiber/context.ts:357:28)
    at FiberContext.evaluateNow (file://.../node_modules/@effect-ts/system/_src/Fiber/context.ts:940:36)
    at file://.../node_modules/@effect-ts/system/_src/Fiber/context.ts:576:33

Fiber: #17 (started at: 2022-09-04T21:49:21.161Z) was supposed to continue to:

  a future continuation at packages/@contentlayer/core/src/generation/generate-dotpkg.ts:216:13

Fiber: #17 (started at: 2022-09-04T21:49:21.161Z) Execution trace:

  packages/@contentlayer/core/src/generation/generate-dotpkg.ts:144:22
  packages/@contentlayer/core/src/generation/generate-dotpkg.ts:143:60
  packages/@contentlayer/core/src/generation/generate-dotpkg.ts:125:10
  packages/@contentlayer/core/src/generation/generate-dotpkg.ts:82:13
  packages/@contentlayer/core/src/generation/generate-dotpkg.ts:78:16
@schickling
Copy link
Collaborator

Thanks for opening this issue. Can you please provide a reproduction repo with instructions how to reproduce the problem you're experiencing? Thank you!

@atahanyorganci
Copy link
Author

I created a simple Nextjs app with default configuration and I am able reproduce the error just clone the repo atahanyorganci/polymorphic and run yarn dev

@schickling
Copy link
Collaborator

Thanks a lot @atahanyorganci. Polymorphic data requires a "type" field value in the data to differentiate between the different possible types.

The following adjustment should make your project work:

CleanShot 2022-09-05 at 14 57 38@2x

However, we should improve the error handling for cases like these in Contentlayer.

@schickling schickling changed the title Encountered an unchecked error while creating polymorphic nested objects Improve error handling for polymorphic field data with missing type value Sep 5, 2022
@atahanyorganci
Copy link
Author

Thanks a lot @atahanyorganci. Polymorphic data requires a "type" field value in the data to differentiate between the different possible types.

The following adjustment should make your project work:

CleanShot 2022-09-05 at 14 57 38@2x

However, we should improve the error handling for cases like these in Contentlayer.

For the last object it should be LinkList but this resolves the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants