Skip to content

Commit

Permalink
beautiful error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 18, 2018
1 parent 1776edf commit 888d186
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
11 changes: 11 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,17 @@ An invalid `options.protocol` was passed.
Both `breakEvalOnSigint` and `eval` options were set in the REPL config, which
is not supported.

<a id="ERR_INVALID_RETURN_PROPERTY"></a>
### ERR_INVALID_RETURN_PROPERTY

Thrown in case a function option does not return an expected property type.

<a id="ERR_INVALID_RETURN_PROPERTY_STRING"></a>
### ERR_INVALID_RETURN_PROPERTY_STRING

Thrown in case a function option does not return an expected string property
type.

<a id="ERR_INVALID_RETURN_VALUE"></a>
### ERR_INVALID_RETURN_VALUE

Expand Down
18 changes: 16 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,29 @@ E('ERR_INVALID_PROTOCOL',
TypeError);
E('ERR_INVALID_REPL_EVAL_CONFIG',
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL', TypeError);
E('ERR_INVALID_RETURN_PROPERTY', (input, name, prop, value) => {
let type;
if (value && value.constructor && value.constructor.name) {
type = `instance of ${value.constructor.name}`;
} else {
type = `type ${typeof value}`;
}
return `Expected ${input} to be returned for the ${prop} from the ` +
`"${name}" function but got ${type}.`;
}, TypeError);
E('ERR_INVALID_RETURN_PROPERTY_STRING', (input, name, prop, value) => {
return `Expected a valid ${input} to be returned for the ${prop} from the ` +
`"${name}" function but got ${value}.`;
}, TypeError);
E('ERR_INVALID_RETURN_VALUE', (input, name, value) => {
let type;
if (value && value.constructor && value.constructor.name) {
type = `instance of ${value.constructor.name}`;
} else {
type = `type ${typeof value}`;
}
return `Expected ${input} to be returned from the "${name}"` +
` function but got ${type}.`;
return `Expected ${input} to be returned from the ` +
`"${name}" function but got ${type}.`;
}, TypeError);
E('ERR_INVALID_SYNC_FORK_INPUT',
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s',
Expand Down
24 changes: 18 additions & 6 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_PROTOCOL,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_STRING,
ERR_MISSING_DYNAMIC_INTSTANTIATE_HOOK,
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
Expand Down Expand Up @@ -57,21 +58,32 @@ class Loader {
await this._resolve(specifier, parentURL, defaultResolve);

if (typeof url !== 'string')
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
throw new ERR_INVALID_RETURN_PROPERTY(
'string', 'loader resolve', 'url', url
);

if (typeof format !== 'string')
throw new ERR_INVALID_ARG_TYPE('format', 'string', format);
throw new ERR_INVALID_RETURN_PROPERTY_STRING(
'string', 'loader resolve', 'format', format
);

if (format === 'builtin')
return { url: `node:${url}`, format };

if (this._resolve !== defaultResolve) {
// throws ERR_INVALID_URL for resolve if not valid
new URL(url);
try {
new URL(url);
} catch (e) {
throw new ERR_INVALID_RETURN_PROPERTY_STRING(
'url', 'loader resolve', 'url', url
);
}
}

if (format !== 'dynamic' && !url.startsWith('file:'))
throw new ERR_INVALID_PROTOCOL(url, 'file:');
throw new ERR_INVALID_RETURN_PROPERTY_STRING(
'file: url', 'loader resolve', 'url', url
);

return { url, format };
}
Expand Down
6 changes: 4 additions & 2 deletions test/es-module/test-esm-loader-invalid-url.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-url.mjs
/* eslint-disable node-core/required-modules */
import assert from 'assert';
import common from '../common';

import('../fixtures/es-modules/test-esm-ok.mjs')
.then(() => {
assert.fail();
}, (err) => {
assert.strictEqual(err.code, 'ERR_INVALID_URL');
});
assert.strictEqual(err.code, 'ERR_INVALID_RETURN_PROPERTY_STRING');
})
.then(common.mustCall());

0 comments on commit 888d186

Please sign in to comment.