Update rejections to use Error
This commit is contained in:
parent
88de427184
commit
6a6e200ef9
@ -57,7 +57,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
|
|||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post');
|
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post');
|
||||||
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
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', () => {
|
xhr.addEventListener('load', () => {
|
||||||
cache[definition.expression] = xhr.responseText;
|
cache[definition.expression] = xhr.responseText;
|
||||||
resolve(xhr.responseText);
|
resolve(xhr.responseText);
|
||||||
@ -87,7 +87,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
|
|||||||
} else {
|
} else {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', `https://jisho.org/search/${definition.expression}`);
|
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', () => {
|
xhr.addEventListener('load', () => {
|
||||||
cache[definition.expression] = xhr.responseText;
|
cache[definition.expression] = xhr.responseText;
|
||||||
resolve(xhr.responseText);
|
resolve(xhr.responseText);
|
||||||
|
@ -402,7 +402,7 @@ function optionsLoad() {
|
|||||||
chrome.storage.local.get(['options'], store => {
|
chrome.storage.local.get(['options'], store => {
|
||||||
const error = chrome.runtime.lastError;
|
const error = chrome.runtime.lastError;
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(new Error(error));
|
||||||
} else {
|
} else {
|
||||||
resolve(store.options);
|
resolve(store.options);
|
||||||
}
|
}
|
||||||
@ -431,7 +431,7 @@ function optionsSave(options) {
|
|||||||
chrome.storage.local.set({options: JSON.stringify(options)}, () => {
|
chrome.storage.local.set({options: JSON.stringify(options)}, () => {
|
||||||
const error = chrome.runtime.lastError;
|
const error = chrome.runtime.lastError;
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(new Error(error));
|
||||||
} else {
|
} else {
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ function requestJson(url, action, params) {
|
|||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.overrideMimeType('application/json');
|
xhr.overrideMimeType('application/json');
|
||||||
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
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);
|
xhr.open(action, url);
|
||||||
if (params) {
|
if (params) {
|
||||||
xhr.send(JSON.stringify(params));
|
xhr.send(JSON.stringify(params));
|
||||||
@ -34,7 +34,7 @@ function requestJson(url, action, params) {
|
|||||||
return JSON.parse(responseText);
|
return JSON.parse(responseText);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
return Promise.reject('Invalid response');
|
return Promise.reject(new Error('Invalid response'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -46,9 +46,8 @@ class FrontendApiReceiver {
|
|||||||
result => {
|
result => {
|
||||||
this.sendResult(port, id, senderId, {result});
|
this.sendResult(port, id, senderId, {result});
|
||||||
},
|
},
|
||||||
e => {
|
error => {
|
||||||
const error = typeof e.toString === 'function' ? e.toString() : e;
|
this.sendResult(port, id, senderId, {error: errorToJson(error)});
|
||||||
this.sendResult(port, id, senderId, {error});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ class FrontendApiSender {
|
|||||||
|
|
||||||
invoke(action, params, target) {
|
invoke(action, params, target) {
|
||||||
if (this.disconnected) {
|
if (this.disconnected) {
|
||||||
return Promise.reject('Disconnected');
|
return Promise.reject(new Error('Disconnected'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.port === null) {
|
if (this.port === null) {
|
||||||
@ -110,8 +110,8 @@ class FrontendApiSender {
|
|||||||
clearTimeout(info.timer);
|
clearTimeout(info.timer);
|
||||||
info.timer = null;
|
info.timer = null;
|
||||||
|
|
||||||
if (typeof data.error === 'string') {
|
if (typeof data.error !== 'undefined') {
|
||||||
info.reject(data.error);
|
info.reject(jsonToError(data.error));
|
||||||
} else {
|
} else {
|
||||||
info.resolve(data.result);
|
info.resolve(data.result);
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ class FrontendApiSender {
|
|||||||
const info = this.callbacks[id];
|
const info = this.callbacks[id];
|
||||||
delete this.callbacks[id];
|
delete this.callbacks[id];
|
||||||
info.timer = null;
|
info.timer = null;
|
||||||
info.reject(reason);
|
info.reject(new Error(reason));
|
||||||
}
|
}
|
||||||
|
|
||||||
static generateId(length) {
|
static generateId(length) {
|
||||||
|
@ -98,7 +98,7 @@ class PopupProxy {
|
|||||||
|
|
||||||
invokeHostApi(action, params={}) {
|
invokeHostApi(action, params={}) {
|
||||||
if (typeof this.parentFrameId !== 'number') {
|
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}`);
|
return this.apiSender.invoke(action, params, `popup-proxy-host#${this.parentFrameId}`);
|
||||||
}
|
}
|
||||||
|
@ -30,19 +30,19 @@ function utilInvoke(action, params={}) {
|
|||||||
chrome.runtime.sendMessage(data, (response) => {
|
chrome.runtime.sendMessage(data, (response) => {
|
||||||
utilCheckLastError(chrome.runtime.lastError);
|
utilCheckLastError(chrome.runtime.lastError);
|
||||||
if (response !== null && typeof response === 'object') {
|
if (response !== null && typeof response === 'object') {
|
||||||
if (response.error) {
|
if (typeof response.error !== 'undefined') {
|
||||||
reject(response.error);
|
reject(jsonToError(response.error));
|
||||||
} else {
|
} else {
|
||||||
resolve(response.result);
|
resolve(response.result);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const message = response === null ? 'Unexpected null response' : `Unexpected response of type ${typeof response}`;
|
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) {
|
} catch (e) {
|
||||||
window.yomichan_orphaned = true;
|
window.yomichan_orphaned = true;
|
||||||
reject(e.message);
|
reject(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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 = (
|
const EXTENSION_IS_BROWSER_EDGE = (
|
||||||
extensionHasBrowser() &&
|
extensionHasBrowser() &&
|
||||||
(!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined'))
|
(!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined'))
|
||||||
|
Loading…
Reference in New Issue
Block a user