Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obs-store/local-store should upgrade webextension error to real error #7207

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions app/scripts/lib/local-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = class ExtensionStore {
const local = extension.storage.local
return new Promise((resolve, reject) => {
local.get(null, (/** @type {any} */ result) => {
const err = extension.runtime.lastError
const err = checkForError()
if (err) {
reject(err)
} else {
Expand All @@ -69,7 +69,7 @@ module.exports = class ExtensionStore {
const local = extension.storage.local
return new Promise((resolve, reject) => {
local.set(obj, () => {
const err = extension.runtime.lastError
const err = checkForError()
if (err) {
reject(err)
} else {
Expand All @@ -88,3 +88,17 @@ module.exports = class ExtensionStore {
function isEmpty (obj) {
return Object.keys(obj).length === 0
}

/**
* Returns an Error if extension.runtime.lastError is present
* this is a workaround for the non-standard error object thats used
* @returns {Error}
*/
function checkForError () {
const lastError = extension.runtime.lastError
if (!lastError) return
// if it quacks like an Error, its an Error
if (lastError.stack && lastError.message) return lastError
// repair incomplete error object (eg chromium v77)
return new Error(lastError.message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should check whether it's already an error first, before extracting the message to construct a new error?

I haven't tested this on Firefox, but MDN does claim that runtime.lastError is an Error object. So maybe an instanceof Error check would catch that case, preserving any other metadata that error might have (like a stack trace).

}