Skip to content

Commit

Permalink
[feat] Friendlier error messages for common config errors (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrygrFlzr authored Jul 15, 2021
1 parent 41da1eb commit 53e9285
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-boats-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

feat(config): Friendlier error messages for common errors
11 changes: 11 additions & 0 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import options from './options.js';
* @returns {any}
*/
function validate(definition, option, keypath) {
if (typeof option !== 'object') {
if (typeof option === 'undefined') {
throw new Error(
'Your config is missing default exports. Make sure to include "export default config;"'
);
} else {
throw new Error(
`Unexpected config type "${typeof option}", make sure your default export is an object.`
);
}
}
for (const key in option) {
if (!(key in definition)) {
let message = `Unexpected option ${keypath}.${key}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'invalid';
30 changes: 30 additions & 0 deletions packages/kit/src/core/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,34 @@ test('load default config (esm)', async () => {
await testLoadDefaultConfig('default-esm');
});

test('errors on loading config without default export', async () => {
let errorMessage = null;
try {
const cwd = join(__dirname, 'fixtures', 'export-missing');
await load_config({ cwd });
} catch (e) {
errorMessage = e.message;
}

assert.equal(
errorMessage,
'Your config is missing default exports. Make sure to include "export default config;"'
);
});

test('errors on loading config with incorrect default export', async () => {
let errorMessage = null;
try {
const cwd = join(__dirname, 'fixtures', 'export-string');
await load_config({ cwd });
} catch (e) {
errorMessage = e.message;
}

assert.equal(
errorMessage,
'Unexpected config type "string", make sure your default export is an object.'
);
});

test.run();

0 comments on commit 53e9285

Please sign in to comment.