Permalink
Browse files

Catch exceptions for cache set, hitting an optional error callback.

  • Loading branch information...
1 parent b430f59 commit 9911e625e48bb2bf82b626b29c7fc1c9e422d3a7 @bshelton229 bshelton229 committed Oct 16, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 jquery-ajax-localstorage-cache.js
@@ -40,7 +40,17 @@ $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.success = function( data ) {
var strdata = data;
if ( this.dataType.indexOf( 'json' ) === 0 ) strdata = JSON.stringify( data );
- localStorage.setItem( cacheKey, strdata );
+
+ // Save the data to localStorage catching exceptions (possibly QUOTA_EXCEEDED_ERR)
+ try {
+ localStorage.setItem( cacheKey, strdata );
+ } catch (e) {
+ // Remove any incomplete data that may have been saved before the exception was caught
+ localStorage.removeItem( cacheKey );
+ localStorage.removeItem( cacheKey + 'cachettl' );
+ if ( options.cacheError ) options.cacheError( e, cacheKey, strdata );
+ }
+
if ( options.realsuccess ) options.realsuccess( data );
};

0 comments on commit 9911e62

Please sign in to comment.