@jhubbardsf/ethers-decode-error
Version:
Decode ethers.js smart contract errors into human-readable messages
3 lines (2 loc) • 2.56 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 t;!function(r){r[r.UserError=0]="UserError",r[r.EmptyError=1]="EmptyError",r[r.RevertError=2]="RevertError",r[r.PanicError=3]="PanicError",r[r.UnknownError=4]="UnknownError",r[r.CustomError=5]="CustomError"}(t||(t={})),r.decodeError=function(r,o){var n,a;if(!(r instanceof Error))return{type:t.UnknownError,error:null!=(n=r.message)?n:"Unexpected error",data:void 0};try{a=function(r){var e,t,o=null!=(e=r.data)?e:null==(t=r.error)?void 0:t.data;if(void 0===o)throw r;var n="string"==typeof o?o:o.data;if("object"==typeof n&&n.data&&(n=n.data),void 0===n||"string"!=typeof n)throw r;return n}(r)}catch(e){return r.message?r.message.includes("user rejected transaction")?{type:t.UserError,error:"User has rejected the transaction",data:a}:{type:t.UnknownError,error:r.message,data:a}:{type:t.UnknownError,error:"Unknown error",data:a}}if("0x"===a)return{type:t.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:t.RevertError,error:s,data:a}}catch(r){return{type:t.UnknownError,error:"Unknown error returned",data:a}}}else{if(!a.startsWith("0x4e487b71")){if(!o)return{type:t.CustomError,error:a.slice(0,10),data:a};var d=(o instanceof e.utils.Interface?o:new e.utils.Interface(o)).parseError(a);return{type:t.CustomError,error:d.name,args:d.args,data:a}}var c=a.slice(10);try{var u,l=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:t.PanicError,error:l,data:a}}catch(r){return{type:t.UnknownError,error:"Unknown panic error",data:a}}}}});
//# sourceMappingURL=decode-error.umd.js.map