@open-rights-exchange/orejs
Version:
Orejs is a Javascript helper library to provide simple high-level access to the ore-protocol. Orejs uses eosJS as a wrapper to the EOS blockchain.
84 lines • 4.04 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
/* eslint-disable no-restricted-syntax */
var RpcError = require('eosjs').RpcError;
var stringifySafe = require('./helpers').stringifySafe;
// subset of errors from EOS chain - https://github.com/EOSIO/eos/blob/master/libraries/chain/include/eosio/chain/exceptions.hpp
// IMPORTANT: These are in order of importance
// ... keep the Misc.. errors at the bottom - they catch the categories if not caught by a more specific error higher up
var ChainError = {
AccountCreationFailedAlreadyExists: 'account_name_exists_exception',
AuthUnsatisfied: 'unsatisfied_authorization',
AuthMissing: 'missing_auth_exception',
BlockDoesNotExist: 'unknown_block_exception',
TxExceededResources: '_exceeded',
PermissionAlreadyLinked: 'Attempting to update required authority, but new requirement is same as old',
PermissionNotLinked: 'Attempting to unlink authority, but no link found',
PermissionDeleteFailedInUse: '(Cannot delete a linked authority. Unlink the authority first|Cannot delete active authority|Cannot delete owner authority)',
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 mapChainError(error) {
var e_1, _a;
var errorSearchString;
var errorMessage;
var newError;
if (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 if (error instanceof Error) {
errorSearchString = error.name + " " + error.message;
errorMessage = errorSearchString;
}
else {
errorSearchString = stringifySafe(error);
errorMessage = errorSearchString;
}
try {
// 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 (var _b = __values(Object.keys(ChainError)), _c = _b.next(); !_c.done; _c = _b.next()) {
var errorKey = _c.value;
var regexp = new RegExp(ChainError[errorKey], 'i');
var match = regexp.exec(errorSearchString);
if (match) {
newError = new Error(errorMessage);
newError.name = errorKey; // example: Error = 'Account_Exists: ChainError: Chain error message'
break;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return newError || error;
}
module.exports = {
ChainError: ChainError,
mapChainError: mapChainError
};
//# sourceMappingURL=errors.js.map