Skip to content

Commit

Permalink
Fix UI thread exception crash (#1650)
Browse files Browse the repository at this point in the history
Fix UI thread exception crash
  • Loading branch information
nopara73 committed Jun 28, 2019
2 parents 6439787 + ac1476d commit 04eb815
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
50 changes: 35 additions & 15 deletions WalletWasabi/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Net.Http;
using WalletWasabi.Helpers;

namespace System
{
public static class ExceptionExtensions
{
public static string ToTypeMessageString(this Exception ex)
{
return $"{ex.GetType().Name}: {ex.Message}";
var trimmed = Guard.Correct(ex.Message);
if (trimmed == "")
{
return ex.GetType().Name;
}
else
{
return $"{ex.GetType().Name}: {ex.Message}";
}
}

public static Dictionary<string, string> BitcoinCoreTransalations { get; } = new Dictionary<string, string> {
["too-long-mempool-chain"] = "At least one coin you are trying to spend is part of long or heavy chain of unconfirmed transactions. You must wait for some previous transactions to confirm.",
["bad-txns-inputs-missingorspent"] = "At least one coin you are trying to spend is already spent.",
["missing-inputs"] = "At least one coin you are trying to spend is already spent.",
["txn-mempool-conflict"] = "At least one coin you are trying to spend is already spent.",
["bad-txns-inputs-duplicate"] = "The transaction contains duplicated inputs.",
["bad-txns-nonfinal"] = "The transaction is not final and cannot be broadcasted.",
["bad-txns-oversize"] = "The transaction is too big."
};

public static string ToUserFriendlyString(this HttpRequestException ex)
{
var transalations = new Dictionary<string, string>
var trimmed = Guard.Correct(ex.Message);
if (trimmed == "")
{
["too-long-mempool-chain"] = "At least one coin you are trying to spend is part of long chain of unconfirmed transactions. You must wait for some previous transactions to confirm.",
["bad-txns-inputs-missingorspent"] = "At least one coin you are trying to spend is already spent.",
["missing-inputs"] = "At least one coin you are trying to spend is already spent.",
["bad-txns-inputs-duplicate"] = "The transaction contains duplicated inputs.",
["bad-txns-nonfinal"] = "The transaction is not final and cannot be broadcasted.",
["bad-txns-oversize"] = "The transaction is too big."
};

var msg = ex.Message.Substring(0, ex.Message.IndexOf(','));
if(transalations.ContainsKey(msg))
return ex.ToTypeMessageString();
}
else
{
return transalations[msg];
foreach (KeyValuePair<string, string> pair in BitcoinCoreTransalations)
{
if (trimmed.Contains(pair.Key, StringComparison.InvariantCultureIgnoreCase))
{
return pair.Value;
}
}

return ex.ToTypeMessageString();
}
return ex.ToTypeMessageString();
}
}
}
12 changes: 5 additions & 7 deletions WalletWasabi/Services/WalletService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,9 +1505,12 @@ public async Task SendTransactionAsync(SmartTransaction transaction)
{
await client.BroadcastAsync(transaction);
}
catch (HttpRequestException ex2) when (ex2.Message.Contains("txn-mempool-conflict", StringComparison.InvariantCultureIgnoreCase))
catch (HttpRequestException ex2) when (
ex2.Message.Contains("bad-txns-inputs-missingorspent", StringComparison.InvariantCultureIgnoreCase)
|| ex2.Message.Contains("missing-inputs", StringComparison.InvariantCultureIgnoreCase)
|| ex2.Message.Contains("txn-mempool-conflict", StringComparison.InvariantCultureIgnoreCase))
{
if (transaction.Transaction.Inputs.Count == 1)
if (transaction.Transaction.Inputs.Count == 1) // If we tried to only spend one coin, then we can mark it as spent. If there were more coins, then we don't know.
{
OutPoint input = transaction.Transaction.Inputs.First().PrevOut;
SmartCoin coin = Coins.FirstOrDefault(x => x.TransactionId == input.Hash && x.Index == input.N);
Expand All @@ -1516,11 +1519,6 @@ public async Task SendTransactionAsync(SmartTransaction transaction)
coin.SpentAccordingToBackend = true;
}
}
throw new HttpRequestException("Coin has been already spent.");
}
catch (HttpRequestException ex2) when (ex2.Message.Contains("too-long-mempool-chain", StringComparison.InvariantCultureIgnoreCase))
{
throw new HttpRequestException("There are too many unconfirmed transactions. Please wait for confirmation.");
}
}

Expand Down

0 comments on commit 04eb815

Please sign in to comment.