Skip to content

Commit

Permalink
Fix unhandled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Acesmndr committed Sep 30, 2019
1 parent 87f9875 commit 327f594
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
15 changes: 11 additions & 4 deletions src/background/methods/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ const getData = (whatToFetch) => {
chromeUtils.sendMessage({
type: 'update',
data: {
todolist: storeData[whatToFetch],
containsItem: !!storeData[whatToFetch].length
todolist: storeData[whatToFetch] || [],
containsItem: storeData[whatToFetch] && !!storeData[whatToFetch].length
}
});
}).catch((error) => {
reject(error);
chromeUtils.notify({
title: 'Error fetching store data',
message: error
});
});
}

/* clears local storage */
const reset = () => {
chromeUtils.clearCache();
return new Promise((resolve, reject) => {
chromeUtils.clearCache().then(() => resolve());
}).catch((error) => {
reject();
});
}

/* saves data in local storage and returns a promise */
Expand Down
1 change: 0 additions & 1 deletion src/background/plugins/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ export default function() {
default:
console.warn('Invalid request type', request.type);
}
return;
});
}
19 changes: 12 additions & 7 deletions src/chrome/chrome-utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
/* global chrome */
const clearCache = () => {
chrome.storage.local.clear();
return new Promise((resolve) => {
chrome.storage.local.clear(() => {
resolve();
});
});
}

/* fetch returns a promise which resolves with the data from storage */
const fetch = (getWhat) => {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
chrome.storage.local.get(getWhat,(data) => {
resolve(data);
});
});
};
}

/* triggers a chrome notification */
const notify = (params) => {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
chrome.notifications.create({
type: 'basic',
title: params.title || 'Chrome Extension',
Expand All @@ -28,8 +32,9 @@ const notify = (params) => {

/* dispatch a message to listeners in background or popup */
const sendMessage = (msg) => {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(msg, resolve);
return new Promise((resolve) => {
chrome.runtime.sendMessage(msg);
resolve();
});
}

Expand All @@ -46,7 +51,7 @@ const setBadgeText = (text = 'ext') => {
chrome.browserAction.setBadgeText({
text: text,
});
};
}

/*
sets up the context menu of the browser action
Expand Down
3 changes: 2 additions & 1 deletion src/popup/plugins/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export default function (component) {
chrome.runtime.onMessage.addListener((request) => {
if (component.isMounted === false) {
return false;
return component;
}
switch (request.type) {
case 'update':
Expand All @@ -15,6 +15,7 @@ export default function (component) {
break;
default:
}
return false;
});

return component;
Expand Down
5 changes: 3 additions & 2 deletions test/base-request-api.specs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global define, it, describe, before, after, beforeEach, afterEach */
import { assert } from 'chai';
import sinon from 'sinon';
import { assert } from 'chai';
import * as baseApi from '../src/background/api/base-request-api';

let server;
const xhr = sinon.useFakeXMLHttpRequest();
global.XMLHttpRequest = xhr;

describe('Base Api Request module', () => {
beforeEach(() => {
server = sinon.fakeServer.create();
Expand All @@ -16,7 +17,7 @@ describe('Base Api Request module', () => {
chrome.flush();
});
describe('request function', () => {
it('should set the error and return error response object if the response status is invalid', () => {
it('should set the error and return error response object if the response status is invalid', (done) => {
server.respond('GET', 'url', [404, { 'Content-Type': 'application/json' }, JSON.stringify('Not Found')]);
baseApi.request('GET', 'url', {}).then((response) => {
assert.deepEqual(response, { status: 404, error: true });
Expand Down

0 comments on commit 327f594

Please sign in to comment.