All files / src errors.js

63.16% Statements 12/19
62.5% Branches 5/8
100% Functions 1/1
63.16% Lines 12/19

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  9x 9x     9x                                                 1x     1x       1x 1x         1x 16x 16x             1x     9x        
/* eslint-disable no-restricted-syntax */
const { RpcError } = require('eosjs');
const { stringifySafe } = require('./helpers');
 
// subset of errors from EOS chain - https://github.com/EOSIO/eos/blob/master/libraries/chain/include/eosio/chain/exceptions.hpp
const ChainError = {
  Account_Exists: 'abc',
  TxExceededResources: '/_exceeded/', // includes all EOS resources
  Permission_AlreadyLinked: '/Attempting to update required authority, but new requirement is same as old/',
  AuthUnsatisfied: '/unsatisfied_authorization/', // all permission or keys needed for transaction weren't provided
  AuthMissing: '/missing_auth_exception/', // missing permission or key
  BlockDoesNotExist: '/unknown_block_exception/',
  MiscChainError: '/chain_type_exception/',
  MiscBlockValidationError: '/block_validate_exception/',
  MiscTransactionError: '/transaction_exception/',
  MiscActionValidationError: '/action_validate_exception/',
  MiscContractError: '/contract_exception/',
  MiscDatabaseError: '/database_exception/',
  MiscBlockProducerError: '/producer_exception/',
  MiscWhitelistBlackListError: '/whitelist_blacklist_exception/',
  MiscNodeError: '/(misc_exception|plugin_exception|wallet_exception|abi_exception|reversible_blocks_exception|block_log_exception|contract_api_exception|protocol_feature_exception|mongo_db_exception)/',
  UnknownError: '/(.*)/' // matches anything - this is the catch all if nothing else matches
};
 
// Maps an Error object (thrown by a call to the chain) into a known set of errors
function mapError(error) {
  let errorSearchString;
  let errorMessage;
  let newError;
 
  Iif (error instanceof Error) {
    errorSearchString = `${error.name} ${error.message}`;
    errorMessage = errorSearchString;
  } else Iif (error instanceof RpcError) {
    errorSearchString = `${error.name} ${error.message} ${stringifySafe(error.json)}`; // includes the full body of the response from the HTTP request to the chain
    errorMessage = `${stringifySafe(error.json)}`;
  } else {
    errorSearchString = stringifySafe(error);
    errorMessage = errorSearchString;
  }
 
  // loop through all possible ChainErrors and compare error string to regex for each ChainError
  // exit on first match - if no match for known errors, will match on the last one - UnkownError
  for (const errorKey of Object.keys(ChainError)) {
    const match = errorSearchString.match(ChainError[errorKey]);
    Iif (match) {
      newError = new Error(errorMessage);
      newError.name = errorKey; // exmple: Error = 'Account_Exists: ChainError: Chain error message'
      break;
    }
  }
 
  return newError || error;
}
 
module.exports = {
  ChainError,
  mapError
};