zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
113 lines • 3.06 kB
JavaScript
import { TransactionStatus, ZkVerifyEvents } from "../../../enums.js";
export const decodeDispatchError = (api, err) => {
if (err.isModule) {
try {
const meta = api.registry.findMetaError(err.asModule);
const docs = (meta.docs?.join(' ') || '').trim();
return {
code: `${meta.section}.${meta.name}`,
message: docs ? docs : `${meta.section}.${meta.name}`,
section: meta.section
};
} catch {
const mod = err.asModule;
return {
code: `Module(${mod.index.toString()}:${mod.error.toString()})`,
message: 'Module error'
};
}
}
if (err.isToken) {
return {
code: `Token.${err.asToken.type}`,
message: `Token.${err.asToken.type}`
};
}
if (err.isArithmetic) {
return {
code: `Arithmetic.${err.asArithmetic.type}`,
message: `Arithmetic.${err.asArithmetic.type}`
};
}
if (err.isBadOrigin) {
return {
code: 'BadOrigin',
message: 'BadOrigin'
};
}
if (err.isCannotLookup) {
return {
code: 'CannotLookup',
message: 'CannotLookup'
};
}
if (err.isOther) {
return {
code: 'Other',
message: 'Other'
};
}
const extended = err;
if (extended.isTransactional) {
const kind = extended.asTransactional?.type ?? 'Unknown';
return {
code: `Transactional.${kind}`,
message: `Transactional.${kind}`
};
}
if (extended.isUnavailable) {
return {
code: 'Unavailable',
message: 'Unavailable'
};
}
if (extended.isExhausted) {
return {
code: 'Exhausted',
message: 'Exhausted'
};
}
if (extended.isCorruption) {
return {
code: 'Corruption',
message: 'Corruption'
};
}
return {
message: err.toString()
};
};
export const handleError = (emitter, api, transactionInfo, error, shouldThrow = true, status) => {
let decodedError;
const hasProofType = info => 'proofType' in info;
if (error instanceof Error) {
try {
const parsedError = JSON.parse(error.message);
if (parsedError.module && parsedError.module.index !== undefined) {
const dispatchError = api.registry.createType('DispatchError', parsedError);
decodedError = decodeDispatchError(api, dispatchError).message;
} else {
decodedError = error.message;
}
} catch {
decodedError = error.message;
}
} else {
decodedError = decodeDispatchError(api, error).message;
}
if (status && status.isInvalid && transactionInfo.status !== TransactionStatus.Finalized) {
transactionInfo.status = TransactionStatus.Invalid;
decodedError = 'Transaction was marked as invalid.';
} else {
transactionInfo.status = TransactionStatus.Error;
}
if (emitter.listenerCount(ZkVerifyEvents.ErrorEvent) > 0) {
emitter.emit(ZkVerifyEvents.ErrorEvent, {
...(hasProofType(transactionInfo) && {
proofType: transactionInfo.proofType
}),
error: decodedError
});
}
if (shouldThrow) throw new Error(decodedError);
};