(error, opt_associatedElement)
| 152 | * @return {!Error} |
| 153 | */ |
| 154 | export 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; |
no test coverage detected