diff --git a/plugins/JsonRPCServer.js b/plugins/JsonRPCServer.js index 2559b808..3a829101 100644 --- a/plugins/JsonRPCServer.js +++ b/plugins/JsonRPCServer.js @@ -29,29 +29,37 @@ function blockchainRPC() { } }, getBlockInfo: async (args, callback) => { - const { blockNumber } = args; - - if (Number.isInteger(blockNumber)) { - const block = await database.getBlockInfo(blockNumber); - callback(null, block); - } else { - callback({ - code: 400, - message: 'missing or wrong parameters: blockNumber is required', - }, null); + try { + const { blockNumber } = args; + + if (Number.isInteger(blockNumber)) { + const block = await database.getBlockInfo(blockNumber); + callback(null, block); + } else { + callback({ + code: 400, + message: 'missing or wrong parameters: blockNumber is required', + }, null); + } + } catch (error) { + callback(error, null); } }, getTransactionInfo: async (args, callback) => { - const { txid } = args; - - if (txid && typeof txid === 'string') { - const transaction = await database.getTransactionInfo(txid); - callback(null, transaction); - } else { - callback({ - code: 400, - message: 'missing or wrong parameters: txid is required', - }, null); + try { + const { txid } = args; + + if (txid && typeof txid === 'string') { + const transaction = await database.getTransactionInfo(txid); + callback(null, transaction); + } else { + callback({ + code: 400, + message: 'missing or wrong parameters: txid is required', + }, null); + } + } catch (error) { + callback(error, null); } }, getStatus: async (args, callback) => { @@ -88,72 +96,84 @@ function blockchainRPC() { function contractsRPC() { return { getContract: async (args, callback) => { - const { name } = args; - - if (name && typeof name === 'string') { - const contract = await database.findContract({ name }); - callback(null, contract); - } else { - callback({ - code: 400, - message: 'missing or wrong parameters: contract is required', - }, null); + try { + const { name } = args; + + if (name && typeof name === 'string') { + const contract = await database.findContract({ name }); + callback(null, contract); + } else { + callback({ + code: 400, + message: 'missing or wrong parameters: contract is required', + }, null); + } + } catch (error) { + callback(error, null); } }, findOne: async (args, callback) => { - const { contract, table, query } = args; - - if (contract && typeof contract === 'string' - && table && typeof table === 'string' - && query && typeof query === 'object') { - const result = await database.findOne({ - contract, - table, - query, - }); - - callback(null, result); - } else { - callback({ - code: 400, - message: 'missing or wrong parameters: contract and tableName are required', - }, null); + try { + const { contract, table, query } = args; + + if (contract && typeof contract === 'string' + && table && typeof table === 'string' + && query && typeof query === 'object') { + const result = await database.findOne({ + contract, + table, + query, + }); + + callback(null, result); + } else { + callback({ + code: 400, + message: 'missing or wrong parameters: contract and tableName are required', + }, null); + } + } catch (error) { + callback(error, null); } }, find: async (args, callback) => { - const { - contract, - table, - query, - limit, - offset, - indexes, - } = args; - - if (contract && typeof contract === 'string' - && table && typeof table === 'string' - && query && typeof query === 'object') { - const lim = limit || 1000; - const off = offset || 0; - const ind = indexes || []; - - const result = await database.find({ + try { + const { contract, table, query, - limit: lim, - offset: off, - indexes: ind, - }); - - callback(null, result); - } else { - callback({ - code: 400, - message: 'missing or wrong parameters: contract and tableName are required', - }, null); + limit, + offset, + indexes, + } = args; + + if (contract && typeof contract === 'string' + && table && typeof table === 'string' + && query && typeof query === 'object') { + const lim = limit || 1000; + const off = offset || 0; + const ind = indexes || []; + + const result = await database.find({ + contract, + table, + query, + limit: lim, + offset: off, + indexes: ind, + }); + + callback(null, result); + } else { + callback({ + code: 400, + message: 'missing or wrong parameters: contract and tableName are required', + }, null); + } + } catch (error) { + callback(error, null); } }, }; diff --git a/plugins/P2P.js b/plugins/P2P.js index f66f0689..c0c3add7 100644 --- a/plugins/P2P.js +++ b/plugins/P2P.js @@ -444,7 +444,13 @@ const proposeRoundHandler = async (args, callback) => { function p2p() { return { - proposeRoundHash: (args, callback) => proposeRoundHandler(args, callback), + proposeRoundHash: (args, callback) => { + try { + proposeRoundHandler(args, callback); + } catch (error) { + callback(error, null); + } + }, }; }