Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC error handling #88

Merged
merged 2 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 95 additions & 75 deletions plugins/JsonRPCServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
}
},
};
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