Skip to content

Commit af1b791

Browse files
committed
Log validation errors
1 parent 89ad7e0 commit af1b791

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

lib/init-action.js

Lines changed: 17 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config-utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ async function downloadCacheWithTime(
541541
}
542542

543543
async function loadUserConfig(
544+
logger: Logger,
544545
configFile: string,
545546
workspacePath: string,
546547
apiDetails: api.GitHubApiCombinedDetails,
@@ -557,9 +558,9 @@ async function loadUserConfig(
557558
);
558559
}
559560
}
560-
return getLocalConfig(configFile);
561+
return getLocalConfig(logger, configFile);
561562
} else {
562-
return await getRemoteConfig(configFile, apiDetails);
563+
return await getRemoteConfig(logger, configFile, apiDetails);
563564
}
564565
}
565566

@@ -816,6 +817,7 @@ export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
816817
} else {
817818
logger.debug(`Using configuration file: ${inputs.configFile}`);
818819
userConfig = await loadUserConfig(
820+
logger,
819821
inputs.configFile,
820822
inputs.workspacePath,
821823
inputs.apiDetails,
@@ -913,18 +915,23 @@ function isLocal(configPath: string): boolean {
913915
return configPath.indexOf("@") === -1;
914916
}
915917

916-
function getLocalConfig(configFile: string): UserConfig {
918+
function getLocalConfig(logger: Logger, configFile: string): UserConfig {
917919
// Error if the file does not exist
918920
if (!fs.existsSync(configFile)) {
919921
throw new ConfigurationError(
920922
errorMessages.getConfigFileDoesNotExistErrorMessage(configFile),
921923
);
922924
}
923925

924-
return parseUserConfig(configFile, fs.readFileSync(configFile, "utf-8"));
926+
return parseUserConfig(
927+
logger,
928+
configFile,
929+
fs.readFileSync(configFile, "utf-8"),
930+
);
925931
}
926932

927933
async function getRemoteConfig(
934+
logger: Logger,
928935
configFile: string,
929936
apiDetails: api.GitHubApiCombinedDetails,
930937
): Promise<UserConfig> {
@@ -963,6 +970,7 @@ async function getRemoteConfig(
963970
}
964971

965972
return parseUserConfig(
973+
logger,
966974
configFile,
967975
Buffer.from(fileContents, "base64").toString("binary"),
968976
);

src/config/db-config.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import test, { ExecutionContext } from "ava";
22

33
import { RepositoryProperties } from "../feature-flags/properties";
44
import { KnownLanguage, Language } from "../languages";
5+
import { getRunnerLogger } from "../logging";
6+
import {
7+
checkExpectedLogMessages,
8+
getRecordingLogger,
9+
LoggedMessage,
10+
} from "../testing-utils";
511
import { ConfigurationError, prettyPrintPack } from "../util";
612

713
import * as dbConfig from "./db-config";
@@ -394,6 +400,7 @@ test(
394400

395401
test("parseUserConfig - successfully parses valid YAML", (t) => {
396402
const result = dbConfig.parseUserConfig(
403+
getRunnerLogger(true),
397404
"test",
398405
`
399406
paths-ignore:
@@ -418,6 +425,7 @@ test("parseUserConfig - throws a ConfigurationError if the file is not valid YAM
418425
t.throws(
419426
() =>
420427
dbConfig.parseUserConfig(
428+
getRunnerLogger(true),
421429
"test",
422430
`
423431
paths-ignore:
@@ -431,3 +439,27 @@ test("parseUserConfig - throws a ConfigurationError if the file is not valid YAM
431439
},
432440
);
433441
});
442+
443+
test("parseUserConfig - throws a ConfigurationError if validation fails", (t) => {
444+
const loggedMessages: LoggedMessage[] = [];
445+
const logger = getRecordingLogger(loggedMessages);
446+
447+
t.throws(
448+
() =>
449+
dbConfig.parseUserConfig(
450+
logger,
451+
"test",
452+
`
453+
paths-ignore:
454+
- "some/path"
455+
queries: true
456+
`,
457+
),
458+
{
459+
instanceOf: ConfigurationError,
460+
},
461+
);
462+
463+
const expectedMessages = ["instance.queries is not of a type(s) array"];
464+
checkExpectedLogMessages(t, loggedMessages, expectedMessages);
465+
});

src/config/db-config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,14 @@ export function generateCodeScanningConfig(
480480
/**
481481
* Attempts to parse `contents` into a `UserConfig` value.
482482
*
483+
* @param logger The logger to use.
483484
* @param pathInput The path to the file where `contents` was obtained from, for use in error messages.
484485
* @param contents The string contents of a YAML file to try and parse as a `UserConfig`.
485486
* @returns The `UserConfig` corresponding to `contents`, if parsing was successful.
486487
* @throws A `ConfigurationError` if parsing failed.
487488
*/
488489
export function parseUserConfig(
490+
logger: Logger,
489491
pathInput: string,
490492
contents: string,
491493
): UserConfig {
@@ -498,10 +500,13 @@ export function parseUserConfig(
498500
const result = new jsonschema.Validator().validate(doc, schema);
499501

500502
if (result.errors.length > 0) {
503+
for (const error of result.errors) {
504+
logger.error(error.stack);
505+
}
501506
throw new ConfigurationError(
502507
errorMessages.getInvalidConfigFileMessage(
503508
pathInput,
504-
`The configuration file contained ${result.errors.length} error(s)`,
509+
`There are ${result.errors.length} error(s)`,
505510
),
506511
);
507512
}

0 commit comments

Comments
 (0)