Catching requests when offline

I wanted to bulletproof some code by creating a local copy of my data and grabbing that local copy when the phone is offline (like when I’m on a plane).

I’ve thrown a “try” around my Request, but I get an error when I test (instead of failing over to the “catch”):

	let req;

	try{
		req = await new Request(url);
	}catch(e){
		console.log("Offline mode");
	}

I never get to the logged error, instead I get a “The Internet connection appears to be offline.”

Has anyone else encountered/solved this?

you won’t catch the error on the new Request. you’ll need to do that on the req.loadXXX methods.

var url = 'https://jsonplaceholder.typicode.com/todos/'

try {
  var req = new Request(url)
} catch(e) {
  log('fail on new request')
}

try {
  var data = await req.loadJSON()
} catch(e) {
  log('fail on load')
}

log(data)
1 Like