throw "string" in a Plugin
cfjedimaster opened this issue · comments
Describe the bug
This could be a bug, or a mistake on my part, or a documentation issue. I'm working on a test plugin (not for anything serious, but to test something else). My plugin requires an option named key. So I used code like this:
if(!options) options = {};
if(!options.key) {
throw 'API key required for weather plugin.';
}
When I run eleventy
, my expectation is an error, but I'd expect to see the message in the output. Instead I got:
I'd expect the error to be shown even w/o debug turned on, but if I do turn it on, I don't get any additional information. I do see this:
Eleventy:UserConfig Adding plugin (unknown name: check your config file). +5ms
Which begs the question, where would the name be defined. My plugin does have a package.json, but as it's not in npm now (nor will it be), I'm loading it like so:
const weatherPlugin = require('../weatherplugin/');
So am I doing something wrong, or is this a bug where Eleventy doesn't properly reshow the error? Also, why doesn't Eleventy know the name of my plugin? I can open a second bug, but if the docs (https://www.11ty.dev/docs/plugins/) could talk a bit more about creating plugins, it would be helpful.
To Reproduce
Steps to reproduce the behavior:
- Go to '...'
- Click on '....'
- Scroll down to '....'
- See error
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment:
- OS and Version: [e.g. Windows/Mac/Linux]
- Eleventy Version [via
eleventy --version
ornpx @11ty/eleventy --version
]
Additional context
Add any other context about the problem here.
@cfjedimaster You find all the fun stuff!
I might defer to https://eslint.org/docs/rules/no-throw-literal for the intelligent answer, but I can reproduce the error locally when throwing literals, but not an Error object.
Here is my tweaked ./plugins/bad-plugin.js from #1313 (comment):
module.exports = (eleventyConfig = {}, options = {}) => {
console.log(`I'm a custom plugin: ${__filename}`);
if (!options.key) {
throw "API key required for weather plugin.";
};
eleventyConfig.addFilter("customFilter", (value) => `plugin: ${value}`);
};
Running eleventy, here's my output:
> eleventy
I'm a config file: /private/tmp/11ty-global-help/.eleventy.js
I'm a custom plugin: /private/tmp/11ty-global-help/plugins/bad-plugin.js
[11ty] Eleventy CLI Fatal Error: (more in DEBUG output)
[11ty] > (No error message provided)
`undefined` was thrown
[11ty]
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! 11ty-global-help@1.0.0 eleventy: `eleventy`
npm ERR! Exit status 1
BUT if I change it to throw an Error object, then I think I get the output you want/expected:
module.exports = (eleventyConfig = {}, options = {}) => {
console.log(`I'm a custom plugin: ${__filename}`);
if (!options.key) {
- throw "API key required for weather plugin.";
+ throw new Error("API key required for weather plugin.");
};
eleventyConfig.addFilter("customFilter", (value) => `plugin: ${value}`);
};
OUTPUT
> eleventy
I'm a config file: /private/tmp/11ty-global-help/.eleventy.js
I'm a custom plugin: /private/tmp/11ty-global-help/plugins/bad-plugin.js
[11ty] Eleventy CLI Fatal Error: (more in DEBUG output)
[11ty] > API key required for weather plugin.
`Error` was thrown:
[11ty] Error: API key required for weather plugin.
at module.exports (/private/tmp/11ty-global-help/plugins/bad-plugin.js:4:11)
at UserConfig._executePlugin (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/UserConfig.js:335:7)
at /private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:187:23
at Array.forEach (<anonymous>)
at TemplateConfig.processPlugins (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:185:29)
at TemplateConfig.mergeConfig (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:253:10)
at TemplateConfig.getConfig (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:117:26)
at new Eleventy (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/Eleventy.js:74:39)
at Object.<anonymous> (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/cmd.js:70:14)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
TLDR: Use throw new Error("<string>");
not throw "<string>";
.
Eleventy:UserConfig Adding plugin (unknown name: check your config file). +5ms
Looks like that error string is hardcoded and wrapped in a TODO.
Lines 328 to 330 in a9c29f4
Which explains why I get the error w/ anonymous arrow functions or named functions or trying to module.exports
a function and then also shimming in module.exports.name = "BadPlugin";
. 🤷
(Using 1.0.0-beta.4)
I'm not building a 'real' plugin, but I'll change to throwing an object now that this is logged and confirmed as a real issue. :)
Good call on the Error object throw, thanks everyone!
I did ship an improvement to debug output to show plugin.name
that will go out with beta.9, thanks y’all!