From 826e1a3f89e053ea7dd1abd9681d4c399a59714c Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Tue, 20 Sep 2022 03:50:40 +0000 Subject: [PATCH] General: Throw a more descriptive error when templates are not found. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `wp.template()` is called in JavaScript for a template element that does not exist, a very nondescript error message is currently returned (“Uncaught TypeError: Cannot read property `replace` of `undefined`”). This updates adds a check for this scenario and a new “Template not found” error is now thrown instead. Props joehoyle, noisysocks, hilayt24. Fixes #36631. git-svn-id: https://develop.svn.wordpress.org/trunk@54241 602fd350-edb4-49c9-b593-d223f7449a82 --- src/js/_enqueues/wp/util.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/js/_enqueues/wp/util.js b/src/js/_enqueues/wp/util.js index 7aedbbb584..c603bc69f4 100644 --- a/src/js/_enqueues/wp/util.js +++ b/src/js/_enqueues/wp/util.js @@ -36,6 +36,9 @@ window.wp = window.wp || {}; }; return function ( data ) { + if ( ! document.getElementById( 'tmpl-' + id ) ) { + throw new Error( 'Template not found: ' + '#tmpl-' + id ); + } compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options ); return compiled( data ); };