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

Side by Side Diff: polyfill.js

Issue 29582713: Issue 4579 - Wrap rejection reason in Error object (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Oct. 19, 2017, 12:29 a.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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 { 53 {
54 object = object[node]; 54 object = object[node];
55 55
56 if (!object) 56 if (!object)
57 return; 57 return;
58 } 58 }
59 59
60 let func = object[name]; 60 let func = object[name];
61 object[name] = function(...args) 61 object[name] = function(...args)
62 { 62 {
63 let callStack = new Error().stack;
64
63 if (typeof args[args.length - 1] == "function") 65 if (typeof args[args.length - 1] == "function")
64 return func.apply(object, args); 66 return func.apply(object, args);
65 67
66 // If the last argument is undefined, we drop it from the list assuming 68 // If the last argument is undefined, we drop it from the list assuming
67 // it stands for the optional callback. We must do this, because we have 69 // it stands for the optional callback. We must do this, because we have
68 // to replace it with our own callback. If we simply append our own 70 // to replace it with our own callback. If we simply append our own
69 // callback to the list, it won't match the signature of the function and 71 // callback to the list, it won't match the signature of the function and
70 // will cause an exception. 72 // will cause an exception.
71 if (typeof args[args.length - 1] == "undefined") 73 if (typeof args[args.length - 1] == "undefined")
72 args.pop(); 74 args.pop();
73 75
74 return new Promise((resolve, reject) => 76 return new Promise((resolve, reject) =>
75 { 77 {
76 func.call(object, ...args, result => 78 func.call(object, ...args, result =>
77 { 79 {
78 let error = browser.runtime.lastError; 80 let error = browser.runtime.lastError;
79 if (error) 81 if (error)
82 {
83 // runtime.lastError on Chrome is a plain object with only a
84 // message property.
85 if (!(error instanceof Error))
Sebastian Noack 2017/10/19 04:30:24 Why checking whether it is an error, if we know th
Manish Jethani 2017/10/19 09:35:39 It seems that only Chrome's runtime.lastError is n
Manish Jethani 2017/10/19 09:37:39 Ollie just confirmed on IRC that it is in fact an
86 error = new Error(error.message);
87
88 // Add a more helpful stack trace.
89 error.stack = callStack.replace(/^Error\n {4}at Object\./,
Manish Jethani 2017/10/19 00:32:13 The stack trace from the caller's side is actually
Sebastian Noack 2017/10/19 04:30:24 Not sure whether this is worth it: * Firefox does
Wladimir Palant 2017/10/19 07:51:35 Making assumptions about the stack format and doin
Manish Jethani 2017/10/19 09:35:39 I think I see that this was a bit of overkill. I'v
90 "Error\n at browser.");
80 reject(error); 91 reject(error);
92 }
81 else 93 else
94 {
82 resolve(result); 95 resolve(result);
96 }
83 }); 97 });
84 }); 98 });
85 }; 99 };
100
101 // Set the name for debugging.
102 Object.defineProperty(object[name], "name", {
Manish Jethani 2017/10/19 00:32:13 If we do this we get the name of the API in the st
Sebastian Noack 2017/10/19 04:30:24 IMO this goes to far. Not even the original chrome
Manish Jethani 2017/10/19 09:35:39 Yes, but when the call throws an error synchronous
103 writable: false,
104 enumerable: false,
105 configurable: true,
106 value: api
107 });
86 } 108 }
87 109
88 function shouldWrapAPIs() 110 function shouldWrapAPIs()
89 { 111 {
90 try 112 try
91 { 113 {
92 return !(browser.storage.local.get([]) instanceof Promise); 114 return !(browser.storage.local.get([]) instanceof Promise);
93 } 115 }
94 catch (error) 116 catch (error)
95 { 117 {
(...skipping 16 matching lines...) Expand all
112 134
113 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList 135 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
114 // didn't have iterator support before Chrome 51. 136 // didn't have iterator support before Chrome 51.
115 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 137 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
116 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) 138 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
117 { 139 {
118 if (!(Symbol.iterator in object.prototype)) 140 if (!(Symbol.iterator in object.prototype))
119 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; 141 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
120 } 142 }
121 } 143 }
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