Skip to content

Commit d0236b3

Browse files
committed
Make config validation error messages more consistent
1 parent fd8ff36 commit d0236b3

1 file changed

Lines changed: 25 additions & 22 deletions

File tree

‎apps/api-extractor/src/api/ExtractorConfig.ts‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ export class ExtractorConfig {
275275
do {
276276
// Check if this file was already processed.
277277
if (visitedPaths.has(currentConfigFilePath)) {
278-
throw new Error(`The API Extractor config files contain a cycle. "${currentConfigFilePath}"`
279-
+ ` is included twice. Please check the "extends" values in config files.`);
278+
throw new Error(`The API Extractor "extends" setting contains a cycle.`
279+
+ ` This file is included twice: "${currentConfigFilePath}"`);
280280
}
281281
visitedPaths.add(currentConfigFilePath);
282282

@@ -424,7 +424,7 @@ export class ExtractorConfig {
424424

425425
if (options.configObjectFullPath) {
426426
if (!path.isAbsolute(options.configObjectFullPath)) {
427-
throw new Error('configObjectFullPath must be an absolute path');
427+
throw new Error('The "configObjectFullPath" setting must be an absolute path');
428428
}
429429
}
430430

@@ -435,10 +435,10 @@ export class ExtractorConfig {
435435
if (packageJsonFullPath) {
436436
if (!/.json$/i.test(packageJsonFullPath)) {
437437
// Catch common mistakes e.g. where someone passes a folder path instead of a file path
438-
throw new Error('The packageJsonFullPath does not have a .json file extension');
438+
throw new Error('The "packageJsonFullPath" setting does not have a .json file extension');
439439
}
440440
if (!path.isAbsolute(packageJsonFullPath)) {
441-
throw new Error('packageJsonFullPath must be an absolute path');
441+
throw new Error('The "packageJsonFullPath" setting must be an absolute path');
442442
}
443443

444444
if (!options.packageJson) {
@@ -456,14 +456,16 @@ export class ExtractorConfig {
456456

457457
if (!configObject.projectFolder) {
458458
// A merged configuration should have this
459-
throw new Error('The "projectFolder" field is missing');
459+
throw new Error('The "projectFolder" setting is missing');
460460
}
461461

462462
let projectFolder: string;
463463
if (configObject.projectFolder.trim() === '<lookup>') {
464464
if (!options.configObjectFullPath) {
465-
throw new Error('The "<lookup>" token cannot be expanded because configObjectFullPath was not specified');
465+
throw new Error('The "projectFolder" setting uses the "<lookup>" token, but it cannot be expanded because'
466+
+ ' the "configObjectFullPath" setting was not specified');
466467
}
468+
467469
// "The default value for `projectFolder` is the token `<lookup>`, which means the folder is determined
468470
// by traversing parent folders, starting from the folder containing api-extractor.json, and stopping
469471
// at the first folder that contains a tsconfig.json file. If a tsconfig.json file cannot be found in
@@ -478,7 +480,7 @@ export class ExtractorConfig {
478480
}
479481
const parentFolder: string = path.dirname(currentFolder);
480482
if (parentFolder === '' || parentFolder === currentFolder) {
481-
throw new Error('The projectFolder was set to "<lookup>", but a tsconfig.json file cannot be'
483+
throw new Error('The "projectFolder" setting uses the "<lookup>" token, but a tsconfig.json file cannot be'
482484
+ ' found in this folder or any parent folder.');
483485
}
484486
currentFolder = parentFolder;
@@ -487,7 +489,7 @@ export class ExtractorConfig {
487489
ExtractorConfig._rejectAnyTokensInPath(configObject.projectFolder, 'projectFolder');
488490

489491
if (!FileSystem.exists(configObject.projectFolder)) {
490-
throw new Error('The specified projectFolder does not exist: ' + configObject.projectFolder);
492+
throw new Error('The specified "projectFolder" path does not exist: ' + configObject.projectFolder);
491493
}
492494

493495
projectFolder = configObject.projectFolder;
@@ -512,11 +514,11 @@ export class ExtractorConfig {
512514
configObject.mainEntryPointFile, tokenContext);
513515

514516
if (!ExtractorConfig.hasDtsFileExtension(mainEntryPointFile)) {
515-
throw new Error('The "mainEntryPointFile" is not a declaration file: ' + mainEntryPointFile);
517+
throw new Error('The "mainEntryPointFile" value is not a declaration file: ' + mainEntryPointFile);
516518
}
517519

518520
if (!FileSystem.exists(mainEntryPointFile)) {
519-
throw new Error('The "mainEntryPointFile" does not exist: ' + mainEntryPointFile);
521+
throw new Error('The "mainEntryPointFile" path does not exist: ' + mainEntryPointFile);
520522
}
521523

522524
const tsconfigFilePath: string = ExtractorConfig._resolvePathWithTokens('tsconfigFilePath',
@@ -527,7 +529,7 @@ export class ExtractorConfig {
527529
throw new Error('Either the "tsconfigFilePath" or "overrideTsconfig" setting must be specified');
528530
}
529531
if (!FileSystem.exists(configObject.compiler.tsconfigFilePath)) {
530-
throw new Error('The "tsconfigFilePath" does not exist: ' + configObject.compiler.tsconfigFilePath);
532+
throw new Error('The "tsconfigFilePath" path does not exist: ' + configObject.compiler.tsconfigFilePath);
531533
}
532534
}
533535

@@ -542,11 +544,11 @@ export class ExtractorConfig {
542544

543545
if (!reportFilename) {
544546
// A merged configuration should have this
545-
throw new Error('reportFilename is missing');
547+
throw new Error('The "reportFilename" setting is missing');
546548
}
547549
if (reportFilename.indexOf('/') >= 0 || reportFilename.indexOf('\\') >= 0) {
548550
// A merged configuration should have this
549-
throw new Error(`The reportFilename contains invalid characters: "${reportFilename}"`);
551+
throw new Error(`The "reportFilename" setting contains invalid characters: "${reportFilename}"`);
550552
}
551553

552554
const reportFolder: string = ExtractorConfig._resolvePathWithTokens('reportFolder',
@@ -576,11 +578,11 @@ export class ExtractorConfig {
576578

577579
if (tsdocMetadataFilePath.trim() === '<lookup>') {
578580
if (!packageJson) {
579-
throw new Error('The "<lookup>" token cannot be used with compiler.projectFolder because'
580-
+ 'the "packageJson" option was not provided');
581+
throw new Error('The "<lookup>" token cannot be used with the "tsdocMetadataFilePath" setting because'
582+
+ ' the "packageJson" option was not provided');
581583
}
582584
if (!packageJsonFullPath) {
583-
throw new Error('The "<lookup>" token cannot be used with compiler.projectFolder because'
585+
throw new Error('The "<lookup>" token cannot be used with "tsdocMetadataFilePath" because'
584586
+ 'the "packageJsonFullPath" option was not provided');
585587
}
586588
tsdocMetadataFilePath = PackageMetadataManager.resolveTsdocMetadataPath(
@@ -593,7 +595,8 @@ export class ExtractorConfig {
593595
}
594596

595597
if (!tsdocMetadataFilePath) {
596-
throw new Error('The tsdocMetadata.enabled was specified, but tsdocMetadataFilePath is not specified');
598+
throw new Error('The "tsdocMetadata.enabled" setting is enabled,'
599+
+ ' but "tsdocMetadataFilePath" is not specified');
597600
}
598601
}
599602
}
@@ -666,12 +669,12 @@ export class ExtractorConfig {
666669

667670
if (value.indexOf(projectFolderToken) >= 0) {
668671
// If after all replacements, "<projectFolder>" appears somewhere in the string, report an error
669-
throw new Error(`The ${fieldName} value incorrectly uses the "<projectFolder>" token.`
672+
throw new Error(`The "${fieldName}" value incorrectly uses the "<projectFolder>" token.`
670673
+ ` It must appear at the start of the string.`);
671674
}
672675

673676
if (value.indexOf('<lookup>') >= 0) {
674-
throw new Error(`The ${fieldName} value incorrectly uses the "<lookup>" token`);
677+
throw new Error(`The "${fieldName}" value incorrectly uses the "<lookup>" token`);
675678
}
676679
ExtractorConfig._rejectAnyTokensInPath(value, fieldName);
677680
}
@@ -698,8 +701,8 @@ export class ExtractorConfig {
698701
const tokenRegExp: RegExp = /(\<[^<]*?\>)/;
699702
const match: RegExpExecArray | null = tokenRegExp.exec(value);
700703
if (match) {
701-
throw new Error(`The ${fieldName} value contains an unrecognized token "${match[1]}"`);
704+
throw new Error(`The "${fieldName}" value contains an unrecognized token "${match[1]}"`);
702705
}
703-
throw new Error(`The ${fieldName} value contains extra token characters ("<" or ">"): ${value}`);
706+
throw new Error(`The "${fieldName}" value contains extra token characters ("<" or ">"): ${value}`);
704707
}
705708
}

0 commit comments

Comments
 (0)