document on error occurred js code example

Example 1: javascript throw new error

throw new Error("Error message here"); // Uncaught Error: Error message here

Example 2: js catch all images errors

document.addEventListener('error', (e) => {
    console.log('Document error triggered!')
    if (!e.target) {
      return console.log('Received event without target!')
    }
    const errorTarget = e.target
    const imageErrorSource = errorTarget instanceof HTMLImageElement

    if (!imageErrorSource) return false

    return true
  }, true)