Update rejections to use Error

This commit is contained in:
toasted-nutbread 2019-10-07 21:04:58 -04:00
parent 88de427184
commit 6a6e200ef9
8 changed files with 32 additions and 18 deletions

View File

@ -57,7 +57,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.addEventListener('error', () => reject('Failed to scrape audio data'));
xhr.addEventListener('error', () => reject(new Error('Failed to scrape audio data')));
xhr.addEventListener('load', () => {
cache[definition.expression] = xhr.responseText;
resolve(xhr.responseText);
@ -87,7 +87,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
} else {
const xhr = new XMLHttpRequest();
xhr.open('GET', `https://jisho.org/search/${definition.expression}`);
xhr.addEventListener('error', () => reject('Failed to scrape audio data'));
xhr.addEventListener('error', () => reject(new Error('Failed to scrape audio data')));
xhr.addEventListener('load', () => {
cache[definition.expression] = xhr.responseText;
resolve(xhr.responseText);

View File

@ -402,7 +402,7 @@ function optionsLoad() {
chrome.storage.local.get(['options'], store => {
const error = chrome.runtime.lastError;
if (error) {
reject(error);
reject(new Error(error));
} else {
resolve(store.options);
}
@ -431,7 +431,7 @@ function optionsSave(options) {
chrome.storage.local.set({options: JSON.stringify(options)}, () => {
const error = chrome.runtime.lastError;
if (error) {
reject(error);
reject(new Error(error));
} else {
resolve();
}

View File

@ -22,7 +22,7 @@ function requestJson(url, action, params) {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.addEventListener('error', () => reject('Failed to connect'));
xhr.addEventListener('error', () => reject(new Error('Failed to connect')));
xhr.open(action, url);
if (params) {
xhr.send(JSON.stringify(params));
@ -34,7 +34,7 @@ function requestJson(url, action, params) {
return JSON.parse(responseText);
}
catch (e) {
return Promise.reject('Invalid response');
return Promise.reject(new Error('Invalid response'));
}
});
}

View File

@ -46,9 +46,8 @@ class FrontendApiReceiver {
result => {
this.sendResult(port, id, senderId, {result});
},
e => {
const error = typeof e.toString === 'function' ? e.toString() : e;
this.sendResult(port, id, senderId, {error});
error => {
this.sendResult(port, id, senderId, {error: errorToJson(error)});
});
}

View File

@ -31,7 +31,7 @@ class FrontendApiSender {
invoke(action, params, target) {
if (this.disconnected) {
return Promise.reject('Disconnected');
return Promise.reject(new Error('Disconnected'));
}
if (this.port === null) {
@ -110,8 +110,8 @@ class FrontendApiSender {
clearTimeout(info.timer);
info.timer = null;
if (typeof data.error === 'string') {
info.reject(data.error);
if (typeof data.error !== 'undefined') {
info.reject(jsonToError(data.error));
} else {
info.resolve(data.result);
}
@ -122,7 +122,7 @@ class FrontendApiSender {
const info = this.callbacks[id];
delete this.callbacks[id];
info.timer = null;
info.reject(reason);
info.reject(new Error(reason));
}
static generateId(length) {

View File

@ -98,7 +98,7 @@ class PopupProxy {
invokeHostApi(action, params={}) {
if (typeof this.parentFrameId !== 'number') {
return Promise.reject('Invalid frame');
return Promise.reject(new Error('Invalid frame'));
}
return this.apiSender.invoke(action, params, `popup-proxy-host#${this.parentFrameId}`);
}

View File

@ -30,19 +30,19 @@ function utilInvoke(action, params={}) {
chrome.runtime.sendMessage(data, (response) => {
utilCheckLastError(chrome.runtime.lastError);
if (response !== null && typeof response === 'object') {
if (response.error) {
reject(response.error);
if (typeof response.error !== 'undefined') {
reject(jsonToError(response.error));
} else {
resolve(response.result);
}
} else {
const message = response === null ? 'Unexpected null response' : `Unexpected response of type ${typeof response}`;
reject(`${message} (${JSON.stringify(data)})`);
reject(new Error(`${message} (${JSON.stringify(data)})`));
}
});
} catch (e) {
window.yomichan_orphaned = true;
reject(e.message);
reject(e);
}
});
}

View File

@ -53,6 +53,21 @@ function extensionHasBrowser() {
}
}
function errorToJson(error) {
return {
name: error.name,
message: error.message,
stack: error.stack
};
}
function jsonToError(jsonError) {
const error = new Error(jsonError.message);
error.name = jsonError.name;
error.stack = jsonError.stack;
return error;
}
const EXTENSION_IS_BROWSER_EDGE = (
extensionHasBrowser() &&
(!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined'))