+ .then(async (response) => {
+ if (!((response.status >= 200 && response.status < 300) || response.status === 404)) {
+ error_store.set(`Error fetching data from ${api_url} (${response.statusText})`);
+ return undefined;
+ }
+
+ // Parse JSON here instead of returning a promise so we can catch possible
+ // errors.
+ var data;
+ try {
+ data = await response.json();
+ } catch (err) {
+ // e.g. 'JSON.parse: unexpected non-whitespace character after JSON data at line 1'
+ error_store.set(`Error parsing JSON data from ${api_url} (${err})`);
+ return undefined;
+ }
+ return data;
+ })
+ .then((data) => {
+ if (data) {
+ if (data.error) {
+ error_store.set(data.error.message);
+ }
+ callback(data);