* Returns a new Error configured to the format ethers emits errors, with * the %%message%%, [[api:ErrorCode]] %%code%% and additional properties * for the corresponding EthersError. * * Each error in ethers includes the version of ethers, a * machine-readable [[ErrorCode]], and depending o
(message, code, info)
| 98 | * ethers version, %%code%% and all additional properties, serialized. |
| 99 | */ |
| 100 | function makeError(message, code, info) { |
| 101 | let shortMessage = message; |
| 102 | { |
| 103 | const details = []; |
| 104 | if (info) { |
| 105 | if ("message" in info || "code" in info || "name" in info) { |
| 106 | throw new Error(`value will overwrite populated values: ${stringify(info)}`); |
| 107 | } |
| 108 | for (const key in info) { |
| 109 | if (key === "shortMessage") { |
| 110 | continue; |
| 111 | } |
| 112 | const value = (info[key]); |
| 113 | // try { |
| 114 | details.push(key + "=" + stringify(value)); |
| 115 | // } catch (error: any) { |
| 116 | // console.log("MMM", error.message); |
| 117 | // details.push(key + "=[could not serialize object]"); |
| 118 | // } |
| 119 | } |
| 120 | } |
| 121 | details.push(`code=${code}`); |
| 122 | details.push(`version=${_version_js_1.version}`); |
| 123 | if (details.length) { |
| 124 | message += " (" + details.join(", ") + ")"; |
| 125 | } |
| 126 | } |
| 127 | let error; |
| 128 | switch (code) { |
| 129 | case "INVALID_ARGUMENT": |
| 130 | error = new TypeError(message); |
| 131 | break; |
| 132 | case "NUMERIC_FAULT": |
| 133 | case "BUFFER_OVERRUN": |
| 134 | error = new RangeError(message); |
| 135 | break; |
| 136 | default: |
| 137 | error = new Error(message); |
| 138 | } |
| 139 | (0, properties_js_1.defineProperties)(error, { code }); |
| 140 | if (info) { |
| 141 | Object.assign(error, info); |
| 142 | } |
| 143 | if (error.shortMessage == null) { |
| 144 | (0, properties_js_1.defineProperties)(error, { shortMessage }); |
| 145 | } |
| 146 | return error; |
| 147 | } |
| 148 | exports.makeError = makeError; |
| 149 | /** |
| 150 | * Throws an EthersError with %%message%%, %%code%% and additional error |