Skip to content

Commit

Permalink
feat: improve exception handling and assertions for better bug reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
pviotti committed Dec 1, 2021
1 parent 71800ee commit b5bbeb5
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Degiro.Account/Degiro.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Degiro

open System
open System.Diagnostics
open System.Text
open System.Text.RegularExpressions

Expand Down Expand Up @@ -59,7 +60,9 @@ module Account =
allRows
|> Seq.filter (fun x -> Regex.IsMatch(x.Description, txnDescriptionRegExp))

assert (Seq.length descRows >= 1)
Trace.Assert(Seq.length descRows >= 1,
"This transaction could not be parsed - please report this issue on GitHub:\n" +
$"%A{fst txn} - %A{Seq.toList allRows}")

let getTxnTypeAndCurrency (row: Row) =
let matches =
Expand Down Expand Up @@ -126,9 +129,8 @@ module Account =
|> Seq.map getFractionalPrice
|> Seq.sum

assert (totBuyQuantity = totSellQuantity)
assert (totalPriceBuys = totalPriceSells)
assert (degiroFees = 0.0m)
Trace.Assert(totBuyQuantity = totSellQuantity && totalPriceBuys = totalPriceSells && degiroFees = 0.0m,
$"Anomalous cancelled transaction: %A{fst txn} - %A{Seq.toList allRows}")
0.0m, 0.0m, 0
else
let totQuantity = getTotQuantity descRows (fun _ -> true)
Expand Down Expand Up @@ -168,7 +170,7 @@ module Account =
Value = totValue
ValueCurrency = valueCurrency
OrderId = (Option.defaultValue Guid.Empty firstDescRow.OrderId) }
with ex -> failwithf $"Error: %A{Seq.last allRows} - %s{ex.Message} \n%A{ex}"
with ex -> failwithf $"parsing failed on transaction: %A{Seq.toList allRows}\n\n%A{ex}"


/// Get all sell transactions for the given year and Irish tax period
Expand Down Expand Up @@ -201,7 +203,7 @@ module Account =
if quantityToSell = 0 then
totBuyPrice
elif List.isEmpty buys && quantityToSell <> 0 then // Should not happen
failwithf $"can't find buy transactions for remaining {quantityToSell} sells of %A{sellTxn}"
failwithf $"could not find buy transactions for remaining {quantityToSell} sells of %A{sellTxn}"
else
let currBuy = List.head buys

Expand Down

0 comments on commit b5bbeb5

Please sign in to comment.