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