From e3af1383d8ba5c127a1e19187081353565e673f8 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 8 Jun 2022 14:58:52 -0700 Subject: [PATCH] fix: handle malformed manifest JSON and throw ConfigurationError (#1469) * test: add test for malformed manifest config and versions files * fix: handle malformed manifest JSON and throw ConfigurationError --- src/manifest.ts | 12 ++++++++++++ test/manifest.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/manifest.ts b/src/manifest.ts index 4b39ea912..d9cb00178 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -1247,6 +1247,12 @@ async function fetchManifestConfig( 'base', `${github.repository.owner}/${github.repository.repo}` ); + } else if (e instanceof SyntaxError) { + throw new ConfigurationError( + `Failed to parse manifest config JSON: ${configFile}\n${e.message}`, + 'base', + `${github.repository.owner}/${github.repository.repo}` + ); } throw e; } @@ -1302,6 +1308,12 @@ async function fetchReleasedVersions( 'base', `${github.repository.owner}/${github.repository.repo}` ); + } else if (e instanceof SyntaxError) { + throw new ConfigurationError( + `Failed to parse manifest versions JSON: ${manifestFile}\n${e.message}`, + 'base', + `${github.repository.owner}/${github.repository.repo}` + ); } throw e; } diff --git a/test/manifest.ts b/test/manifest.ts index 401d8ac34..19f9694f2 100644 --- a/test/manifest.ts +++ b/test/manifest.ts @@ -628,6 +628,55 @@ describe('Manifest', () => { await Manifest.fromManifest(github, github.repository.defaultBranch); }, ConfigurationError); }); + + it('should throw a configuration error for a malformed manifest config', async () => { + const getFileContentsStub = sandbox.stub( + github, + 'getFileContentsOnBranch' + ); + getFileContentsStub + .withArgs('release-please-config.json', 'main') + .resolves(buildGitHubFileRaw('{"malformed json"')) + .withArgs('.release-please-manifest.json', 'main') + .resolves( + buildGitHubFileContent( + fixturesPath, + 'manifest/versions/versions.json' + ) + ); + await assert.rejects( + async () => { + await Manifest.fromManifest(github, github.repository.defaultBranch); + }, + e => { + console.log(e); + return e instanceof ConfigurationError && e.message.includes('parse'); + } + ); + }); + + it('should throw a configuration error for a malformed manifest config', async () => { + const getFileContentsStub = sandbox.stub( + github, + 'getFileContentsOnBranch' + ); + getFileContentsStub + .withArgs('release-please-config.json', 'main') + .resolves( + buildGitHubFileContent(fixturesPath, 'manifest/config/config.json') + ) + .withArgs('.release-please-manifest.json', 'main') + .resolves(buildGitHubFileRaw('{"malformed json"')); + await assert.rejects( + async () => { + await Manifest.fromManifest(github, github.repository.defaultBranch); + }, + e => { + console.log(e); + return e instanceof ConfigurationError && e.message.includes('parse'); + } + ); + }); }); describe('fromConfig', () => {