Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
How to handle errors when localStorage permission is not granted?

I came across this interview question which asked me the question how do you handle errors when localStorage permission is denied.

I researched through docs and found that such an issue could arise if user has setup browser to deny use of storing data. This results in code throwing SecurityError

I tried to come up with a solution by wrapping localStorage methods viz. setItem() & getItem() into try{..} catch(e){...} blocks.

I was curious to know if a browser fails to give access to localStorage What other options do I have to store my data so that I can gracefully handle this scenario ?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

function CheckStorageAllowance() {
  let storage;
  try {
    storage = window["localStorage"];
    let x = '__storage_test__';
    storage.setItem(x, x);
    storage.removeItem(x);
    return true;
  } catch (e) {
    return e instanceof window.DOMException && (e.code === 22 || e.code === 1014 || e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') && (storage && storage.length !== 0);
  }
}
let check = CheckStorageAllowance();
if (check) {
  // allowed
  console.log("Works");
} else {
  if (!check) {
    console.log("disabled");
    // Disabled
  } else {
    // quota exceeded 
    console.log(check);
  }
}

So essentially we check if we can set an item, remove it fast and return true, if not we determine what the exception was and as you may see there's few potential problems that can occur. Good luck.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!