@jhubbardsf/ethers-decode-error
Version:
Decode ethers.js smart contract errors into human-readable messages
3 lines (2 loc) • 2.68 kB
JavaScript
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("ethers")):"function"==typeof define&&define.amd?define(["exports","ethers"],e):e((r||self).ethersDecodeError={},r.ethers)}(this,function(r,e){var o;r.ErrorType=void 0,(o=r.ErrorType||(r.ErrorType={}))[o.UserError=0]="UserError",o[o.EmptyError=1]="EmptyError",o[o.RevertError=2]="RevertError",o[o.PanicError=3]="PanicError",o[o.UnknownError=4]="UnknownError",o[o.CustomError=5]="CustomError",r.decodeError=function(o,t){var n,a;if(!(o instanceof Error))return{type:r.ErrorType.UnknownError,error:null!=(n=o.message)?n:"Unexpected error",data:void 0};try{a=function(r){var e,o,t=null!=(e=r.data)?e:null==(o=r.error)?void 0:o.data;if(void 0===t)throw r;var n="string"==typeof t?t:t.data;if("object"==typeof n&&n.data&&(n=n.data),void 0===n||"string"!=typeof n)throw r;return n}(o)}catch(e){return o.message?o.message.includes("user rejected transaction")?{type:r.ErrorType.UserError,error:"User has rejected the transaction",data:a}:{type:r.ErrorType.UnknownError,error:o.message,data:a}:{type:r.ErrorType.UnknownError,error:"Unknown error",data:a}}if("0x"===a)return{type:r.ErrorType.EmptyError,error:"Empty error data returned",data:a};if(a.startsWith("0x08c379a0")){var i=a.slice(10);try{var s=e.utils.defaultAbiCoder.decode(["string"],"0x"+i)[0];return{type:r.ErrorType.RevertError,error:s,data:a}}catch(e){return{type:r.ErrorType.UnknownError,error:"Unknown error returned",data:a}}}else{if(!a.startsWith("0x4e487b71")){if(!t)return{type:r.ErrorType.CustomError,error:a.slice(0,10),data:a};var d=(t instanceof e.utils.Interface?t:new e.utils.Interface(t)).parseError(a);return{type:r.ErrorType.CustomError,error:d.name,args:d.args,data:a}}var c=a.slice(10);try{var u,y=null!=(u=function(r){switch(r.toNumber()){case 1:return"Assertion error";case 17:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 18:return"Division or modulo division by zero";case 33:return"Tried to convert a value into an enum, but the value was too big or negative";case 34:return"Incorrectly encoded storage byte array";case 49:return".pop() was called on an empty array";case 50:return"Array accessed at an out-of-bounds or negative index";case 65:return"Too much memory was allocated, or an array was created that is too large";case 81:return"Called a zero-initialized variable of internal function type";default:return}}(e.utils.defaultAbiCoder.decode(["uint256"],"0x"+c)[0]))?u:"Unknown panic code";return{type:r.ErrorType.PanicError,error:y,data:a}}catch(e){return{type:r.ErrorType.UnknownError,error:"Unknown panic error",data:a}}}}});
//# sourceMappingURL=index.umd.js.map