Skip to content

Commit

Permalink
fixup: wordsmith next<HookName>'s validation error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Jun 4, 2022
1 parent 69eb1b2 commit 910d4ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function nextHookFactory(chain, meta, validate) {
// Update only when hook is invoked to avoid fingering the wrong filePath
meta.hookErrIdentifier = `${hookFilePath} '${hookName}'`;

validate(meta.hookErrIdentifier, args);
validate(`${meta.hookErrIdentifier} hook's ${nextHookName}()`, args);

// Set when next<HookName> is actually called, not just generated.
if (generatedHookIndex === 0) { meta.chainFinished = true; }
Expand Down Expand Up @@ -621,7 +621,7 @@ class ESMLoader {
// non-strings can be coerced to a url string
// validateString() throws a less-specific error
throw new ERR_INVALID_ARG_TYPE(
`${hookErrIdentifier} nextLoad(url)`,
`${hookErrIdentifier} url`,
'a url string',
nextUrl,
);
Expand All @@ -633,14 +633,14 @@ class ESMLoader {
new URL(nextUrl);
} catch {
throw new ERR_INVALID_ARG_VALUE(
`${hookErrIdentifier}s nextLoad(url)`,
`${hookErrIdentifier} url`,
nextUrl,
'should be a url string',
);
}
}

validateObject(ctx, `${hookErrIdentifier}s nextLoad(, context)`);
validateObject(ctx, `${hookErrIdentifier} context`);
};

const nextLoad = nextHookFactory(chain, meta, validate);
Expand Down Expand Up @@ -846,10 +846,10 @@ class ESMLoader {
const validate = (hookErrIdentifier, { 0: suppliedSpecifier, 1: ctx }) => {
validateString(
suppliedSpecifier,
`${hookErrIdentifier}s nextResolve(specifier)`,
`${hookErrIdentifier} specifier`,
); // non-strings can be coerced to a url string

validateObject(ctx, `${hookErrIdentifier}s nextResolve(, context)`);
validateObject(ctx, `${hookErrIdentifier} context`);
};

const nextResolve = nextHookFactory(chain, meta, validate);
Expand Down
15 changes: 5 additions & 10 deletions test/es-module/test-esm-loader-chaining.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ const commonArgs = [
assert.strictEqual(status, 1);
assert.match(stderr, /ERR_INVALID_ARG_TYPE/);
assert.match(stderr, /loader-resolve-bad-next-specifier\.mjs/);
assert.match(stderr, /'resolve'/);
assert.match(stderr, /nextResolve\(specifier\)/);
assert.match(stderr, /'resolve' hook's nextResolve\(\) specifier/);
}

{ // Verify error thrown when invalid `context` argument passed to `nextResolve`
Expand All @@ -330,8 +329,7 @@ const commonArgs = [

assert.match(stderr, /ERR_INVALID_ARG_TYPE/);
assert.match(stderr, /loader-resolve-bad-next-context\.mjs/);
assert.match(stderr, /'resolve'/);
assert.match(stderr, /nextResolve\(, context\)/);
assert.match(stderr, /'resolve' hook's nextResolve\(\) context/);
assert.strictEqual(status, 1);
}

Expand All @@ -350,8 +348,7 @@ const commonArgs = [

assert.match(stderr, /ERR_INVALID_ARG_TYPE/);
assert.match(stderr, /loader-load-bad-next-url\.mjs/);
assert.match(stderr, /'load'/);
assert.match(stderr, /nextLoad\(url\)/);
assert.match(stderr, /'load' hook's nextLoad\(\) url/);
assert.strictEqual(status, 1);
}

Expand All @@ -370,8 +367,7 @@ const commonArgs = [

assert.match(stderr, /ERR_INVALID_ARG_VALUE/);
assert.match(stderr, /loader-load-impersonating-next-url\.mjs/);
assert.match(stderr, /'load'/);
assert.match(stderr, /nextLoad\(url\)/);
assert.match(stderr, /'load' hook's nextLoad\(\) url/);
assert.strictEqual(status, 1);
}

Expand All @@ -390,7 +386,6 @@ const commonArgs = [

assert.match(stderr, /ERR_INVALID_ARG_TYPE/);
assert.match(stderr, /loader-load-bad-next-context\.mjs/);
assert.match(stderr, /'load'/);
assert.match(stderr, /nextLoad\(, context\)/);
assert.match(stderr, /'load' hook's nextLoad\(\) context/);
assert.strictEqual(status, 1);
}

0 comments on commit 910d4ff

Please sign in to comment.