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