Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: polyfill.js

Issue 29686631: Issue 4579, 6343 - Let errors from wrapped API calls bubble up (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Feb. 1, 2018, 7:46 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 return func.apply(object, args); 91 return func.apply(object, args);
92 92
93 // If the last argument is undefined, we drop it from the list assuming 93 // If the last argument is undefined, we drop it from the list assuming
94 // it stands for the optional callback. We must do this, because we have 94 // it stands for the optional callback. We must do this, because we have
95 // to replace it with our own callback. If we simply append our own 95 // to replace it with our own callback. If we simply append our own
96 // callback to the list, it won't match the signature of the function and 96 // callback to the list, it won't match the signature of the function and
97 // will cause an exception. 97 // will cause an exception.
98 if (typeof args[args.length - 1] == "undefined") 98 if (typeof args[args.length - 1] == "undefined")
99 args.pop(); 99 args.pop();
100 100
101 let resolvePromise = null;
102 let rejectPromise = null;
103
104 func.call(object, ...args, result =>
105 {
106 let error = browser.runtime.lastError;
107 if (error && !portClosedBeforeResponseError.test(error.message))
108 {
109 // runtime.lastError is already an Error instance on Edge, while on
110 // Chrome it is a plain object with only a message property.
111 if (!(error instanceof Error))
112 {
113 error = new Error(error.message);
114
115 // Add a more helpful stack trace.
116 error.stack = callStack;
117 }
118
119 rejectPromise(error);
120 }
121 else
122 {
123 resolvePromise(result);
kzar 2018/02/06 09:49:24 I wonder if it could be possible that resolvePromi
Manish Jethani 2018/02/06 11:51:37 Since the APIs we are wrapping are asynchronous, t
124 }
125 });
126
101 return new Promise((resolve, reject) => 127 return new Promise((resolve, reject) =>
102 { 128 {
103 func.call(object, ...args, result => 129 resolvePromise = resolve;
104 { 130 rejectPromise = reject;
105 let error = browser.runtime.lastError;
106 if (error && !portClosedBeforeResponseError.test(error.message))
107 {
108 // runtime.lastError is already an Error instance on Edge, while on
109 // Chrome it is a plain object with only a message property.
110 if (!(error instanceof Error))
111 {
112 error = new Error(error.message);
113
114 // Add a more helpful stack trace.
115 error.stack = callStack;
116 }
117
118 reject(error);
119 }
120 else
121 {
122 resolve(result);
123 }
124 });
125 }); 131 });
126 }; 132 };
127 133
128 Object.defineProperty(object, name, descriptor); 134 Object.defineProperty(object, name, descriptor);
129 } 135 }
130 136
131 function wrapRuntimeOnMessage() 137 function wrapRuntimeOnMessage()
132 { 138 {
133 let {onMessage} = browser.runtime; 139 let {onMessage} = browser.runtime;
134 let {addListener, removeListener} = onMessage; 140 let {addListener, removeListener} = onMessage;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 228
223 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList 229 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
224 // didn't have iterator support before Chrome 51. 230 // didn't have iterator support before Chrome 51.
225 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 231 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
226 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) 232 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
227 { 233 {
228 if (!(Symbol.iterator in object.prototype)) 234 if (!(Symbol.iterator in object.prototype))
229 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; 235 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
230 } 236 }
231 } 237 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld