Skip to content

Commit

Permalink
Safely handle ParseErrors in installFromSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Sxderp committed Jan 28, 2018
1 parent 7bfda18 commit 7723b60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/bg/user-script-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(function() {

/// Receive a UserScriptInstall message.
window.onUserScriptInstall = async function(message, sender) {
window.onUserScriptInstall = function(message, sender) {
if (message.details) {
let downloader = new Downloader(message.details, sender);
downloader.start(function() {
Expand All @@ -11,7 +11,20 @@ window.onUserScriptInstall = async function(message, sender) {
}
});
} else if (message.source) {
return await UserScriptRegistry.installFromSource(message.source);
// Returning a promise
return UserScriptRegistry.installFromSource(message.source)
.catch(err => {
if (err instanceof ParseError) {
console.warn(err);
chrome.notifications.create({
'type': 'basic',
'title': 'Failed to import script',
'message': err.message,
});
}
// Propagate to the message listener
throw err;
});
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/bg/user-script-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ async function installFromSource(source) {
console.error('Error looking up script!', event);
};
} catch (e) {
console.error('at installFromSource(), db fail:', e);
if (e instanceof ParseError) {
// Noop, will be handled by caller
} else {
console.error('at installFromSource(), db fail:', e);
}
reject(e);
}
});
});
Expand Down
7 changes: 6 additions & 1 deletion src/content/import/import-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ function onImportUserScript(event) {
// Done loading
chrome.runtime.sendMessage(
{'name': 'UserScriptInstall', 'source': fr.result},
uuid => openUserScriptEditor(uuid));
uuid => {
// TODO: When switching to promises use `.catch`
if (!chrome.runtime.lastError) {
openUserScriptEditor(uuid);
}
});
}
};
fr.readAsText(importScript.files[0]);
Expand Down

0 comments on commit 7723b60

Please sign in to comment.