try { localStorage.setItem(key, value); } catch(e) { if (e.code == 22) { // Storage full, maybe notify user or do some clean-up } }