MCPcopy
hub / github.com/ampproject/amphtml / reportError

Function reportError

src/error-reporting.js:154–240  ·  view source on GitHub ↗
(error, opt_associatedElement)

Source from the content-addressed store, hash-verified

152 * @return {!Error}
153 */
154export function reportError(error, opt_associatedElement) {
155 try {
156 // Convert error to the expected type.
157 let isValidError;
158 if (error) {
159 if (error.message !== undefined) {
160 error = duplicateErrorIfNecessary(/** @type {!Error} */ (error));
161 isValidError = true;
162 } else {
163 const origError = error;
164 error = new Error(tryJsonStringify(origError));
165 error.origError = origError;
166 }
167 } else {
168 error = new Error('Unknown error');
169 }
170 // Report if error is not an expected type.
171 if (!isValidError && getMode().localDev && !getMode().test) {
172 setTimeout(function () {
173 const rethrow = new Error(
174 '_reported_ Error reported incorrectly: ' + error
175 );
176 throw rethrow;
177 });
178 }
179
180 if (error.reported) {
181 return /** @type {!Error} */ (error);
182 }
183 error.reported = true;
184
185 // `associatedElement` is used to add the i-amphtml-error class; in
186 // `#development=1` mode, it also adds `i-amphtml-element-error` to the
187 // element and sets the `error-message` attribute.
188 if (error.messageArray) {
189 const elIndex = findIndex(error.messageArray, (item) => item?.tagName);
190 if (elIndex > -1) {
191 error.associatedElement = error.messageArray[elIndex];
192 }
193 }
194 // Update element.
195 const element = opt_associatedElement || error.associatedElement;
196 if (element && element.classList) {
197 element.classList.add('i-amphtml-error');
198 if (getMode().development) {
199 element.classList.add('i-amphtml-element-error');
200 element.setAttribute('error-message', error.message);
201 }
202 }
203
204 // Report to console.
205 if (
206 self.console &&
207 (isUserErrorMessage(error.message) ||
208 !error.expected ||
209 getMode().localDev)
210 ) {
211 const output = console.error || console.log;

Callers 13

buildInternalMethod · 0.90
onMeasureMethod · 0.90
connectedCallbackMethod · 0.90
initMessagingChannel_Method · 0.90
tryUpgradeElementFunction · 0.90
error_Method · 0.90
wrappedMethod · 0.90
startLayoutMethod · 0.90
reportError_Method · 0.90
measureMethod · 0.90
reportErrorForWinFunction · 0.85

Calls 9

getModeFunction · 0.90
findIndexFunction · 0.90
isUserErrorMessageFunction · 0.90
tryJsonStringifyFunction · 0.85
applyMethod · 0.80
addMethod · 0.45
setAttributeMethod · 0.45

Tested by

no test coverage detected