sindresorhus / gulp-nunjucks

Precompile Nunjucks templates
MIT License
152 stars 20 forks source link

Fix for TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object. fixes #22 #24

Closed brokolja closed 6 years ago

brokolja commented 6 years ago

Hi,

I fixed #22 by using the async version of renderString to catch/emit the error in the callback before its thrown. With this solution the gulp task will not stop on error - it will just show a message.

Greetings , Kolja Kutschera

kevva commented 6 years ago

The fix here isn't using the async version, but rather that you're using an empty string when resdoesn't exist (which will result in an empty file). I think we should find out why env.renderString(file.contents.toString(), context) returns undefined sometimes instead.

brokolja commented 6 years ago

In: https://github.com/mozilla/nunjucks/blob/master/src/environment.js at line: 501 you can see that nunjucks throws rendering errors if no callback is used with env.renderString. So we must use env.renderString with callback to catch errors within the callback if we can not change nunjucks sourcecode. Its not possible to catch an error which is thrown within another scope. But your right it seems that using the async nunjucks render function is not a good thing here because were dealing with streams. But even if it is not the best solution - my fix fixed the problem in my gulp-task.

kevva commented 6 years ago

If it throws we'll catch it since we're using it inside a try/catch block.