@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
109 lines • 3.82 kB
JavaScript
;
/**
* Error Parser
* Routes errors to appropriate error classes based on step and context
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.toError = exports.hasMessage = exports.CustomErrorType = exports.StepError = void 0;
exports.createStepError = createStepError;
exports.parseError = parseError;
const SwapError_1 = require("./SwapError");
/**
* Transaction step where error occurred
*/
var StepError;
(function (StepError) {
StepError["NONCE"] = "NonceStepError";
StepError["PAYLOAD"] = "PayloadStepError";
StepError["SIGNATURE"] = "SignatureStepError";
StepError["IGNORED_SIGNATURE"] = "IgnoredSignatureStepError";
StepError["CHECK_FUNDS"] = "CheckFundsStepError";
StepError["LIST_ACCOUNT"] = "ListAccountStepError";
StepError["LIST_CURRENCY"] = "ListCurrencyStepError";
StepError["UNKNOWN_ACCOUNT"] = "UnknownAccountStepError";
StepError["PAYIN_EXTRA_ID"] = "PayinExtraIdStepError";
})(StepError || (exports.StepError = StepError = {}));
/**
* Input for error parsing
*/
var CustomErrorType;
(function (CustomErrorType) {
CustomErrorType["SWAP"] = "swap";
})(CustomErrorType || (exports.CustomErrorType = CustomErrorType = {}));
const DRAWER_CLOSED_ERROR_NAME = "DrawerClosedError";
const isDrawerClosedError = (error) => {
if (error.name === DRAWER_CLOSED_ERROR_NAME) {
return true;
}
const nestedName = error.cause?.name;
return nestedName === DRAWER_CLOSED_ERROR_NAME;
};
/**
* Maps step errors to error constructors
*/
const ErrorMap = {
[StepError.NONCE]: SwapError_1.NonceStepError,
[StepError.PAYLOAD]: SwapError_1.PayloadStepError,
[StepError.SIGNATURE]: SwapError_1.SignatureStepError,
[StepError.IGNORED_SIGNATURE]: SwapError_1.IgnoredSignatureStepError,
[StepError.CHECK_FUNDS]: SwapError_1.NotEnoughFunds,
[StepError.LIST_ACCOUNT]: SwapError_1.ListAccountError,
[StepError.LIST_CURRENCY]: SwapError_1.ListCurrencyError,
[StepError.UNKNOWN_ACCOUNT]: SwapError_1.UnknownAccountError,
[StepError.PAYIN_EXTRA_ID]: SwapError_1.PayinExtraIdError,
};
/**
* Creates a step-specific error by wrapping the original error
*
* @param error - Original error that occurred
* @param step - Step where error occurred (optional)
* @returns Wrapped error or original error if no step specified
*/
function createStepError({ error, step }) {
// If no step specified, return original error
if (!step) {
return error;
}
// Preserve DrawerClosedError so swap cancellations aren't reclassified.
if (isDrawerClosedError(error)) {
return error;
}
// Get error constructor for this step
const ErrorConstructor = ErrorMap[step];
if (!ErrorConstructor) {
return error;
}
// Wrap original error in step-specific error class
return new ErrorConstructor(error);
}
/**
* Parses an error to determine the correct error class to return based on the context.
*/
function parseError({ error, step, customErrorType }) {
if (!step || customErrorType !== CustomErrorType.SWAP) {
return error;
}
return createStepError({ error, step });
}
const hasMessage = (value) => Boolean(value && typeof value === "object" && "message" in value);
exports.hasMessage = hasMessage;
const toError = (value) => {
if (value instanceof Error) {
return value;
}
if ((0, exports.hasMessage)(value) && typeof value.message === "string") {
return new Error(value.message);
}
return new Error(typeof value === "string"
? value
: (() => {
try {
return JSON.stringify(value);
}
catch {
return String(value);
}
})());
};
exports.toError = toError;
//# sourceMappingURL=parser.js.map