Skip to content

Commit 0822fb1

Browse files
committed
Log validation errors
1 parent 913cd47 commit 0822fb1

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
@@ -526,6 +526,7 @@ async function downloadCacheWithTime(
526526
}
527527

528528
async function loadUserConfig(
529+
logger: Logger,
529530
configFile: string,
530531
workspacePath: string,
531532
apiDetails: api.GitHubApiCombinedDetails,
@@ -542,9 +543,9 @@ async function loadUserConfig(
542543
);
543544
}
544545
}
545-
return getLocalConfig(configFile);
546+
return getLocalConfig(logger, configFile);
546547
} else {
547-
return await getRemoteConfig(configFile, apiDetails);
548+
return await getRemoteConfig(logger, configFile, apiDetails);
548549
}
549550
}
550551

@@ -801,6 +802,7 @@ export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
801802
} else {
802803
logger.debug(`Using configuration file: ${inputs.configFile}`);
803804
userConfig = await loadUserConfig(
805+
logger,
804806
inputs.configFile,
805807
inputs.workspacePath,
806808
inputs.apiDetails,
@@ -898,18 +900,23 @@ function isLocal(configPath: string): boolean {
898900
return configPath.indexOf("@") === -1;
899901
}
900902

901-
function getLocalConfig(configFile: string): UserConfig {
903+
function getLocalConfig(logger: Logger, configFile: string): UserConfig {
902904
// Error if the file does not exist
903905
if (!fs.existsSync(configFile)) {
904906
throw new ConfigurationError(
905907
errorMessages.getConfigFileDoesNotExistErrorMessage(configFile),
906908
);
907909
}
908910

909-
return parseUserConfig(configFile, fs.readFileSync(configFile, "utf-8"));
911+
return parseUserConfig(
912+
logger,
913+
configFile,
914+
fs.readFileSync(configFile, "utf-8"),
915+
);
910916
}
911917

912918
async function getRemoteConfig(
919+
logger: Logger,
913920
configFile: string,
914921
apiDetails: api.GitHubApiCombinedDetails,
915922
): Promise<UserConfig> {
@@ -948,6 +955,7 @@ async function getRemoteConfig(
948955
}
949956

950957
return parseUserConfig(
958+
logger,
951959
configFile,
952960
Buffer.from(fileContents, "base64").toString("binary"),
953961
);

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)