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

Better error messaging when a collection fails to return data #3467

Closed
groenroos opened this issue Oct 1, 2024 · 1 comment
Closed

Better error messaging when a collection fails to return data #3467

groenroos opened this issue Oct 1, 2024 · 1 comment
Labels

Comments

@groenroos
Copy link

Is your feature request related to a problem? Please describe.

I spent slightly too long today debugging a mysterious problem with 3.0.0-beta.2:

  Eleventy:TemplateMap Collection: collections.someCollection size: 0 +1ms
[11ty] Problem writing Eleventy templates:
[11ty] Cannot read properties of undefined (reading 'length') (via TypeError)
  Eleventy:EleventyErrorHandler (error stack): TypeError: Cannot read properties of undefined (reading 'length')
  Eleventy:EleventyErrorHandler     at TemplateMap.getUserConfigCollection (file:///node_modules/@11ty/eleventy/src/TemplateMap.js:602:57)
  Eleventy:EleventyErrorHandler     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  Eleventy:EleventyErrorHandler     at async TemplateMap.setCollectionByTagName (file:///node_modules/@11ty/eleventy/src/TemplateMap.js:341:36)
  Eleventy:EleventyErrorHandler     at async TemplateMap.initDependencyMap (file:///node_modules/@11ty/eleventy/src/TemplateMap.js:365:5)
  Eleventy:EleventyErrorHandler     at async TemplateMap.cache (file:///node_modules/@11ty/eleventy/src/TemplateMap.js:419:3)
  Eleventy:EleventyErrorHandler     at async TemplateWriter._createTemplateMap (file:///node_modules/@11ty/eleventy/src/TemplateWriter.js:353:3)
  Eleventy:EleventyErrorHandler     at async TemplateWriter.generateTemplates (file:///node_modules/@11ty/eleventy/src/TemplateWriter.js:383:3)
  Eleventy:EleventyErrorHandler     at async TemplateWriter.write (file:///node_modules/@11ty/eleventy/src/TemplateWriter.js:431:21)
  Eleventy:EleventyErrorHandler     at async Eleventy.executeBuild (file:///node_modules/@11ty/eleventy/src/Eleventy.js:1339:19) +0ms

I tried to look at someCollection to see if there's some quirk there; I thought maybe the size 0 (which is expected) somehow causes an issue.

After inspecting the Eleventy source to understand what TemplateMap.js is trying and failing to do, I understood that it's failing to construct a debug log message (that isn't typically even visible) - and thus, the problem isn't with the collection mentioned in the debug mode log just above this error, but the next one; which in turn doesn't get logged, because the error arises from creating the log message itself.

Turns out, because of a mistake in resolving a merge conflict, the next collection didn't return anything:

eleventyConfig.addCollection("brokenCollection", function(collection) {
	const data = collection.getFilteredByTag("broken");
});

Describe the solution you'd like

This error is ultimately of the PEBKAC variety, but I wish that:

  • Eleventy would be more error-tolerant in constructing this debug message.
  • Eleventy would return a more useful error message in this case, specifically to complain that a user-defined collection fails to return anything.

The above example might be simple to spot, but collections with more complex/async logic might have subtle edge cases where a collection might fail to return something.

No need to delay the 3.0.0 release for this, though! 😅

Describe alternatives you've considered

No response

Additional context

No response

@zachleat
Copy link
Member

Notably, addCollection should support returning anything (including undefined): https://www.11ty.dev/docs/collections-api/#return-values

This is a bug!

Shipping with 3.0.1-alpha.1

@zachleat zachleat added this to the Eleventy 3.0.1 milestone Oct 21, 2024
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