Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Oct 4, 2023
1 parent e46aff8 commit 7c89d0f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions internal/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (HLedgerCLI) Prices(journalPath string) ([]price.Price, error) {

path, err := binary.LookPath("hledger")
if err != nil {
log.Fatal(err)
log.Error(err)
return prices, err
}

var output, error bytes.Buffer
Expand Down Expand Up @@ -251,7 +252,8 @@ func parseHLedgerPrices(output string, defaultCurrency string) ([]price.Price, e
func parseAmount(amount string) (string, decimal.Decimal, error) {
match := regexp.MustCompile(`^(-?[0-9.,]+)([^\d,.-]+|\s*"[^"]+")$|([^\d,.-]+|\s*"[^"]+"\s*)(-?[0-9.,]+)$`).FindStringSubmatch(amount)
if len(match) == 0 {
log.Fatalf("Could not parse amount: <%s>", amount)
log.Errorf("Could not parse amount: <%s>", amount)
return "", decimal.Zero, fmt.Errorf("Could not parse amount: <%s>", amount)
}

if match[1] != "" {
Expand Down Expand Up @@ -293,7 +295,7 @@ func execLedgerCommand(journalPath string, flags []string) ([]*posting.Posting,
var output, error bytes.Buffer
err = utils.Exec(ledgerPath, &output, &error, args...)
if err != nil {
log.Fatal(error.String())
log.Error(error)
return nil, err
}

Expand Down Expand Up @@ -428,7 +430,7 @@ func execHLedgerCommand(journalPath string, prices []price.Price, flags []string
var output, error bytes.Buffer
err = utils.Exec(path, &output, &error, args...)
if err != nil {
log.Fatal(error.String())
log.Error(error)
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func SyncJournal(db *gorm.DB) (string, error) {

prices, err := ledger.Cli().Prices(config.GetJournalPath())
if err != nil {
log.Fatal(err)
return err.Error(), err
}

price.UpsertAllByType(db, config.Unknown, prices)

postings, err := ledger.Cli().Parse(config.GetJournalPath(), prices)
if err != nil {
log.Fatal(err)
return err.Error(), err
}
posting.UpsertAll(db, postings)

Expand Down

0 comments on commit 7c89d0f

Please sign in to comment.