title: Detect "image corrupt or truncated" in Firefox
body: <p>(Pre-emptive strike: If you're tempted to mark this as a duplicate, note that other questions seem to ask "why am I getting this error?" I know why I'm getting this error; I want to know how I can detect the error in my JavaScript code. It only appears in the Firebug console and, of course, is obvious to the user when the image is loaded.)</p>
  
  <p>I am using <a href="https://github.com/scottjehl/picturefill">picturefill</a> for responsive images. I have a callback that is fired for the load event on the images. So the callback runs every time someone resizes the browser window such that a different image is loaded via picturefill.</p>
  
  <p>Inside the callback, I'm converting the image data to a dataURL via canvas so that I can cache the image data in localStorage so its available to the user even when they are offline. </p>
  
  <p><strong>Note the part about "offline". That's why I cannot rely on the browser cache.</strong> And the HTML5 offline application cache doesn't meet my needs because the images are responsive. (See <a href="http://www.alistapart.com/articles/application-cache-is-a-douchebag/">"Application Cache is a Douchebag"</a> for the explanation of the incompatibility of responsive images with HTML offline application cache.) </p>
  
  <p>On Firefox 14.0.1 on a Mac, the load image will fire if I resize the browser to something really big and then resize it back down to something small again before the large image has a chance to fully load. It ends up reporting "Image corrupt or truncated" in the Firebug console, but doesn't throw an exception or trigger an error event. No indication anything is wrong in the code. Just in the Firebug console.  Meanwhile, it stores a truncated image in localStorage.</p>
  
  <p><strong>How can I reliably and efficiently detect this problem within JavaScript so that I don't cache that image?</strong></p>
  
  <p>Here's how I loop through the picturefill divs to find img tags that have been inserted by picturefill:</p>
  
  <pre><code>    var errorLogger = function () {
          window.console.log('Error loading image.');
          this.removeEventListener('load', cacheImage, false);
      };
  
      for( var i = 0, il = ps.length; i &lt; il; i++ ){
          if( ps[ i ].getAttribute( "data-picture" ) !== null ){
  
              image = ps[ i ].getElementsByTagName( "img" )[0];
              if (image) {
                  if ((imageSrc = image.getAttribute("src")) !== null) {
                      if (imageSrc.substr(0,5) !== "data:") {
                          image.addEventListener("load", cacheImage, false);
                          image.addEventListener('error', errorLogger, false);
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>And here's what the <code>cacheImage()</code> callback looks like:</p>
  
  <pre><code>var cacheImage = function () {
      var canvas,
          ctx,
          imageSrc;
  
      imageSrc = this.getAttribute("src");
  
      if ((pf_index.hasOwnProperty('pf_s_' + imageSrc)) ||
          (imageSrc.substr(0,5) === "data:") ||
          (imageSrc === null) || (imageSrc.length === 0)) {
              return;
      }
  
      canvas = w.document.createElement("canvas");
      canvas.width = this.width;
      canvas.height = this.height;
  
      ctx = canvas.getContext("2d");
      ctx.drawImage(this, 0, 0);
      try {
          dataUri = canvas.toDataURL();
      } catch (e) {
          // TODO: Improve error handling here. For now, if canvas.toDataURL()
          //   throws an exception, don't cache the image and move on.
          return;
      }
  
      // Do not cache if the resulting cache item will take more than 128Kb.
      if (dataUri.length &gt; 131072) {
          return;
      }
  
      pf_index["pf_s_"+imageSrc] = 1;
  
      try {
          localStorage.setItem("pf_s_"+imageSrc, dataUri);
          localStorage.setItem("pf_index", JSON.stringify(pf_index));
      } catch (e) {
          // Caching failed. Remove item from index object so next cached item
          //   doesn't wrongly indicate this item was successfully cached.
          delete pf_index["pf_s_"+imageSrc];
      }
  };
  </code></pre>
  
  <p>Lastly, here is the full text of what I am seeing in Firebug with the URL changed to protect the guilty:</p>
  
  <blockquote>
    <p>Image corrupt or truncated: <a href="http://www.example.com/pf/external/imgs/extralarge.png">http://www.example.com/pf/external/imgs/extralarge.png</a></p>
  </blockquote>