Skip to content

Commit e45efe2

Browse files
template: provide more useful error message on duplicate keys in template
Updated the error message thrown when duplicate mapping keys are encountered during template rendering to provide additional context on how to resolve the duplicate key issue.
1 parent 8311749 commit e45efe2

File tree

5 files changed

+318
-51
lines changed

5 files changed

+318
-51
lines changed

lib/github_provider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class GitHubContentsApi {
6868
}
6969
}))
7070
.catch(e => this._handleResponseError(e, `get contents for ${contentPath}`))
71-
.then(resp => JSON.parse(resp.data).content)
71+
.then(resp => resp.data.content)
7272
.then(data => Buffer.from(data, 'base64').toString('utf8'));
7373
}
7474
}

lib/template.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function JsonPostProcessStrategy(rendered) {
201201
if (rendered.trim() === '') {
202202
rendered = '""';
203203
}
204-
return JSON.stringify(yaml.load(rendered) || '', null, 2);
204+
return JSON.stringify(LoadYamlWithDuplicateKeyCheck(rendered) || '', null, 2);
205205
}
206206

207207
/**
@@ -211,7 +211,32 @@ function YamlPostProcessStrategy(rendered) {
211211
if (rendered.trim() === '') {
212212
rendered = '""';
213213
}
214-
return yaml.dump(yaml.load(rendered));
214+
return yaml.dump(LoadYamlWithDuplicateKeyCheck(rendered));
215+
}
216+
217+
/**
218+
* LoadYamlWithDuplicateKeyCheck will load a yaml string
219+
* and throw a custom error response if duplicate mapping keys exist
220+
*/
221+
function LoadYamlWithDuplicateKeyCheck(str) {
222+
const duplicateKeyRegex = /"(.*?)"/g;
223+
try {
224+
return yaml.load(str);
225+
} catch (e) {
226+
if (e.message.match(/^duplicated mapping key/)) {
227+
const seenKeys = new Set();
228+
let match;
229+
// eslint-disable-next-line no-cond-assign
230+
while ((match = duplicateKeyRegex.exec(e.message)) !== null) {
231+
const matchContents = match[1];
232+
if (seenKeys.has(matchContents)) {
233+
throw new Error(`parameters must have unique values. The parameter value "${matchContents}" is duplicated across parameters and you must update the parameter values to ensure they are unique.`);
234+
}
235+
seenKeys.add(matchContents);
236+
}
237+
}
238+
throw e;
239+
}
215240
}
216241

217242
/**

0 commit comments

Comments
 (0)