Fix error handling

This commit is contained in:
Snazzah 2020-08-12 02:38:51 -05:00
parent f9b5603534
commit 0432967787
No known key found for this signature in database
GPG key ID: 5E71D54F3D86282E
4 changed files with 10 additions and 8 deletions

View file

@ -10,8 +10,8 @@ module.exports = class AdminBalance extends Command {
async exec(message) { async exec(message) {
const response = await this.client.lbry.walletBalance(); const response = await this.client.lbry.walletBalance();
if (await this.handleResponse(message, response)) return;
const wallet = await response.json(); const wallet = await response.json();
if (await this.handleResponse(message, response, wallet)) return;
return message.channel.createMessage({ embed: { return message.channel.createMessage({ embed: {
description: `**Available:** ${wallet.result.available} LBC\n\n` + description: `**Available:** ${wallet.result.available} LBC\n\n` +
`Reserved in Supports: ${wallet.result.reserved_subtotals.supports} LBC\n` + `Reserved in Supports: ${wallet.result.reserved_subtotals.supports} LBC\n` +

View file

@ -10,8 +10,8 @@ module.exports = class Deposit extends Command {
async exec(message) { async exec(message) {
const response = await this.client.lbry.listAddresses(); const response = await this.client.lbry.listAddresses();
if (await this.handleResponse(message, response)) return;
const address = await response.json(); const address = await response.json();
if (await this.handleResponse(message, response, address)) return;
return message.channel.createMessage(`Address: ${address.result.items[0].address}`); return message.channel.createMessage(`Address: ${address.result.items[0].address}`);
} }

View file

@ -17,16 +17,17 @@ module.exports = class Withdraw extends Command {
// Check if the balance is more than requested // Check if the balance is more than requested
const balance = await this.client.lbry.walletBalance(); const balance = await this.client.lbry.walletBalance();
if (await this.handleResponse(message, balance)) return; const balanceJSON = await balance.json();
const availableBalance = parseFloat((await balance.json()).result.available); if (await this.handleResponse(message, balance, balanceJSON)) return;
const availableBalance = parseFloat(balanceJSON.result.available);
if (parseFloat(amount) > availableBalance) if (parseFloat(amount) > availableBalance)
return message.channel.createMessage( return message.channel.createMessage(
'There is not enough available LBC in the wallet to send that amount!'); 'There is not enough available LBC in the wallet to send that amount!');
// Send to wallet // Send to wallet
const response = await this.client.lbry.sendToWallet({ amount, to: args[1] }); const response = await this.client.lbry.sendToWallet({ amount, to: args[1] });
if (await this.handleResponse(message, response)) return;
const transaction = await response.json(); const transaction = await response.json();
if (await this.handleResponse(message, response, transaction)) return;
console.debug('withdrew from master wallet', transaction); console.debug('withdrew from master wallet', transaction);
return message.channel.createMessage(`Sent ${parseFloat(amount)} LBC to ${args[1]}.\n` + return message.channel.createMessage(`Sent ${parseFloat(amount)} LBC to ${args[1]}.\n` +
`https://explorer.lbry.com/tx/${transaction.result.txid}`); `https://explorer.lbry.com/tx/${transaction.result.txid}`);

View file

@ -81,10 +81,11 @@ class Command {
/** /**
* @private * @private
*/ */
async handleResponse(message, response) { async handleResponse(message, response, json) {
if (response.status !== 200) { if (!json) json = await response.json();
if (response.status !== 200 || json.error) {
const error = response.status === 500 ? const error = response.status === 500 ?
{ message: 'Internal server error' } : (await response.json()).error; { message: 'Internal server error' } : json.error;
console.error(`SDK error in ${this.name}:${message.author.id}`, response, error); console.error(`SDK error in ${this.name}:${message.author.id}`, response, error);
await message.channel.createMessage(`LBRY-SDK returned ${response.status}.\n${error.message}`); await message.channel.createMessage(`LBRY-SDK returned ${response.status}.\n${error.message}`);
return true; return true;