Skip to content

Commit

Permalink
Merge pull request #88 from donchate/jsonrpc-args
Browse files Browse the repository at this point in the history
RPC error handling
  • Loading branch information
bt-cryptomancer committed Aug 4, 2021
2 parents 2159e7a + 42e7b99 commit 551c4db
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 76 deletions.
170 changes: 95 additions & 75 deletions plugins/JsonRPCServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,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) => {
Expand Down Expand Up @@ -92,72 +100,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);
}
},
};
Expand Down
8 changes: 7 additions & 1 deletion plugins/P2P.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
};
}

Expand Down

0 comments on commit 551c4db

Please sign in to comment.