@biconomy/abstractjs
Version:
SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.
100 lines • 4.13 kB
JavaScript
// Helper functions to extract specific error patterns
const extractFailedOpError = (message) => {
// First try to match AA error message pattern (any AA followed by numbers)
const aaMatch = message.match(/errorArgs=\[.*?,\s*"(AA[0-9]+[^"]+)"/);
if (aaMatch) {
return aaMatch[1];
}
// Try to match AA error message with hex data pattern
const aaHexMatch = message.match(/errorArgs=\[.*?,\s*"AA[0-9]+[^"]+",\s*"(0x[^"]+)"\]/);
if (aaHexMatch && aaHexMatch[1] !== "0x") {
try {
// Extract the hex data starting after the first 32 bytes (position of string)
const hexData = aaHexMatch[1].slice(130);
// Convert hex to ASCII, removing null bytes, control characters, and padding
const decoded = Buffer.from(hexData.replace(/00+$/, ""), "hex")
.toString()
// biome-ignore lint/suspicious/noControlCharactersInRegex: <explanation>
.replace(/[\u0000-\u001F]/g, ""); // Remove all control characters
return decoded;
}
catch {
return null; // Return null to allow fallback to other patterns
}
}
// Original pattern for other error types
const match = message.match(/errorArgs=\[.*?,\s*"([^"]+)"\]/);
return match?.[1] || null;
};
const extractGasLimitError = (message) => {
const match = message.match(/code=([A-Z_]+),\s*version=/);
return match?.[1] || null;
};
const extractRevertError = (message) => {
const match = message.match(/"reason":"([^"]+)"/);
return match?.[1] || null;
};
const handleErrorsArray = (errors) => {
// Handle array of error objects with msg and path properties
if (typeof errors[0] === "object" && (errors[0].msg || errors[0].message)) {
return errors
.map((error) => {
const message = error.message || error.msg;
return error.path && error.path !== ""
? `${error.path}: ${message}`
: message;
})
.join("\n");
}
const errorMessage = String(errors[0]);
// Try to extract error using the same patterns as the main function
return (extractFailedOpError(errorMessage) ||
extractGasLimitError(errorMessage) ||
extractRevertError(errorMessage) ||
errorMessage);
};
const cleanErrorMessage = (message) => {
return message
.replace(/^(Error|Details|Message):\s*/i, "")
.replace(/^error$/i, "")
.trim();
};
export const parseErrorMessage = (error) => {
if (typeof error !== "object" || error === null) {
const cleanedMessage = cleanErrorMessage(String(error));
return (extractFailedOpError(cleanedMessage) ||
extractGasLimitError(cleanedMessage) ||
extractRevertError(cleanedMessage) ||
cleanedMessage);
}
const errorObj = error;
// Handle Across API error
if (errorObj?.type === "AcrossApiError") {
return [errorObj?.type, errorObj?.message].join(": ");
}
// Handle Error instances
if (error instanceof Error) {
const message = String(error.message);
// Try different error patterns
const errorMessage = extractFailedOpError(message) ||
extractGasLimitError(message) ||
extractRevertError(message) ||
message;
if (errorMessage !== message) {
error.message = errorMessage; // Update the original error message
}
return cleanErrorMessage(errorMessage);
}
// Handle object with errors array
if (Array.isArray(errorObj.errors) && errorObj.errors.length > 0) {
return cleanErrorMessage(handleErrorsArray(errorObj.errors));
}
// Handle object with message or statusText
const message = String(errorObj.message || errorObj.statusText || error);
const cleanedMessage = cleanErrorMessage(message);
return (extractFailedOpError(cleanedMessage) ||
extractGasLimitError(cleanedMessage) ||
extractRevertError(cleanedMessage) ||
cleanedMessage);
};
//# sourceMappingURL=parseErrorMessage.js.map