All files / src/TxSubmitProvider cardanoTxSubmitErrorMapper.ts

94.44% Statements 17/18
100% Branches 9/9
100% Functions 2/2
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  18x       18x 6x 1x   5x 2x   3x 1x   2x 1x   1x     18x 6x 5x 1x     1x        
/* eslint-disable wrap-regex */
import { TxSubmissionError, TxSubmissionErrorCode } from '@cardano-sdk/core';
 
// TODO: LW-9890 Map cardano-submit-api to core errors
// For now this is a very superficial mapping, we should improve it in the future
const parseStringishError = (errorData: string) => {
  if (/outsideofvalidity/i.test(errorData)) {
    return new TxSubmissionError(TxSubmissionErrorCode.OutsideOfValidityInterval, null, errorData);
  }
  if (/valuenotconserved/i.test(errorData)) {
    return new TxSubmissionError(TxSubmissionErrorCode.ValueNotConserved, null, errorData);
  }
  if (/nonadacollateral/i.test(errorData)) {
    return new TxSubmissionError(TxSubmissionErrorCode.NonAdaCollateral, null, errorData);
  }
  if (/incompletewithdrawals/i.test(errorData)) {
    return new TxSubmissionError(TxSubmissionErrorCode.IncompleteWithdrawals, null, errorData);
  }
  return null;
};
 
export const mapCardanoTxSubmitError = (errorData: unknown): TxSubmissionError | null => {
  if (typeof errorData === 'string') {
    return parseStringishError(errorData);
  } else if (typeof errorData === 'object' && errorData) {
    // cardano-submit-api started returning json instead of raw string.
    // For the moment, simply stringify it. In the future we may want to make use of it.
    return parseStringishError(JSON.stringify(errorData));
  }
  return null;
};