zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
67 lines (66 loc) • 2.24 kB
JavaScript
import { TransactionStatus, ZkVerifyEvents } from '../../../enums';
export const decodeDispatchError = (api, dispatchError) => {
if (dispatchError.isModule) {
try {
const decoded = api.registry.findMetaError(dispatchError.asModule);
const { docs, name, section } = decoded;
return `${section}.${name}: ${docs.join(' ')}`;
}
catch {
return `Unknown module error: ${dispatchError.toString()}`;
}
}
else if (dispatchError.isToken) {
return `Token error: ${dispatchError.asToken.type}`;
}
else if (dispatchError.isArithmetic) {
return `Arithmetic error: ${dispatchError.asArithmetic.type}`;
}
else {
return dispatchError.toString();
}
};
export const handleError = (emitter, api, transactionInfo, error, shouldThrow = true, status) => {
let decodedError;
const hasProofType = (info) => {
return '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);
}
else {
decodedError = error.message;
}
}
catch {
decodedError = error.message;
}
}
else {
decodedError = decodeDispatchError(api, error);
}
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);
}
};