eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Better error messaging when a collection fails to return data

Open groenroos opened this issue 1 year ago • 0 comments

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

groenroos avatar Oct 01 '24 12:10 groenroos