Skip to content

Commit

Permalink
🐛 Add error messages to thrown bare Error()s in amp-script (ampproj…
Browse files Browse the repository at this point in the history
…ect#28185)

* Add error messages to thrown bare Error()s

* Throw errors with string template literals
  • Loading branch information
rcebulko authored and ldoroshe committed May 8, 2020
1 parent 996d6ab commit 78a5b66
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions extensions/amp-script/0.1/amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,14 @@ export class AmpScript extends AMP.BaseElement {
startsWith(contentType, 'text/javascript')
)
) {
user().error(
// TODO(#24266): Refactor to %s interpolation when error string
// extraction is ready.
throw user().createError(
TAG,
'Same-origin "src" requires "Content-Type: text/javascript" or "Content-Type: application/javascript". ' +
'Fetched source for %s has "Content-Type: %s". ' +
'See https://amp.dev/documentation/components/amp-script/#security-features.',
debugId,
contentType
`Fetched source for ${debugId} has "Content-Type: ${contentType}". ` +
'See https://amp.dev/documentation/components/amp-script/#security-features.'
);
// TODO(#24266): user().createError() messages are not extracted and
// don't perform string substitution.
throw new Error();
}
return response.text();
} else {
Expand Down Expand Up @@ -514,16 +511,13 @@ export class AmpScriptService {
const bytes = utf8Encode(script);
return this.crypto_.sha384Base64(bytes).then((hash) => {
if (!hash || !this.sources_.includes('sha384-' + hash)) {
user().error(
// TODO(#24266): Refactor to %s interpolation when error string
// extraction is ready.
throw user().createError(
TAG,
'Script hash not found. %s must have "sha384-%s" in meta[name="amp-script-src"].' +
' See https://amp.dev/documentation/components/amp-script/#security-features.',
debugId,
hash
`Script hash not found. ${debugId} must have "sha384-${hash}" in meta[name="amp-script-src"].` +
' See https://amp.dev/documentation/components/amp-script/#security-features.'
);
// TODO(#24266): user().createError() messages are not extracted and
// don't perform string substitution.
throw new Error();
}
});
}
Expand Down

0 comments on commit 78a5b66

Please sign in to comment.