Skip to content

Commit

Permalink
error handling feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 18, 2018
1 parent 888d186 commit 233cf2a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,8 @@ E('ERR_INVALID_RETURN_VALUE', (input, name, value) => {
} 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
11 changes: 9 additions & 2 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_STRING,
ERR_INVALID_RETURN_VALUE,
ERR_MISSING_DYNAMIC_INTSTANTIATE_HOOK,
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
Expand Down Expand Up @@ -54,8 +55,14 @@ class Loader {
if (!isMain && typeof parentURL !== 'string')
throw new ERR_INVALID_ARG_TYPE('parentURL', 'string', parentURL);

const { url, format } =
await this._resolve(specifier, parentURL, defaultResolve);
const resolved = await this._resolve(specifier, parentURL, defaultResolve);

if (typeof resolved !== 'object')
throw new ERR_INVALID_RETURN_VALUE(
'object', 'loader resolve', resolved
);

const { url, format } = resolved;

if (typeof url !== 'string')
throw new ERR_INVALID_RETURN_PROPERTY(
Expand Down
15 changes: 7 additions & 8 deletions test/es-module/test-esm-loader-invalid-url.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-url.mjs
/* eslint-disable node-core/required-modules */
import { expectsError, mustCall } from '../common';
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_RETURN_PROPERTY_STRING');
})
.then(common.mustCall());
.then(assert.fail, expectsError({
code: 'ERR_INVALID_RETURN_PROPERTY_STRING',
message: 'Expected a valid url to be returned for the url from the "loader ' +
'resolve" function but got ../fixtures/es-modules/test-esm-ok.mjs.'
}))
.then(mustCall());
2 changes: 1 addition & 1 deletion test/fixtures/es-module-loaders/loader-invalid-url.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export async function resolve(specifier, parentModuleURL, defaultResolve) {
if (parentModuleURL && specifier !== 'assert') {
if (parentModuleURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') {
return {
url: specifier,
format: 'esm'
Expand Down

0 comments on commit 233cf2a

Please sign in to comment.