Skip to content

Commit

Permalink
handle sync error
Browse files Browse the repository at this point in the history
fixes #56
  • Loading branch information
ananthakumaran committed Sep 29, 2023
1 parent fbdecdd commit 374b48e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ var updateCmd = &cobra.Command{
syncAll := !updateJournal && !updateCommodities && !updatePortfolios

if syncAll || updateJournal {
model.SyncJournal(db)
message, err := model.SyncJournal(db)
if err != nil {
log.Fatal(message)
}
}

if syncAll || updateCommodities {
Expand Down
14 changes: 13 additions & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ func AutoMigrate(db *gorm.DB) {
db.AutoMigrate(&cii.CII{})
}

func SyncJournal(db *gorm.DB) {
func SyncJournal(db *gorm.DB) (string, error) {
AutoMigrate(db)
log.Info("Syncing transactions from journal")

errors, _, err := ledger.Cli().ValidateFile(config.GetJournalPath())
if err != nil {
var message string
for _, error := range errors {
message += error.Message + "\n"
}
return message, err
}

prices, err := ledger.Cli().Prices(config.GetJournalPath())
if err != nil {
log.Fatal(err)
Expand All @@ -43,6 +53,8 @@ func SyncJournal(db *gorm.DB) {
log.Fatal(err)
}
posting.UpsertAll(db, postings)

return "", nil
}

func SyncCommodities(db *gorm.DB) {
Expand Down
5 changes: 4 additions & 1 deletion internal/server/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ type SyncRequest struct {

func Sync(db *gorm.DB, request SyncRequest) gin.H {
if request.Journal {
model.SyncJournal(db)
message, err := model.SyncJournal(db)
if err != nil {
return gin.H{"success": false, "message": message}
}
}

if request.Prices {
Expand Down
14 changes: 13 additions & 1 deletion src/lib/components/Actions.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<script lang="ts">
import { ajax } from "$lib/utils";
import * as toast from "bulma-toast";
import { refresh } from "../../store";
let isLoading = false;
async function sync(request: Record<string, any>) {
isLoading = true;
try {
await ajax("/api/sync", { method: "POST", body: JSON.stringify(request) });
const { success, message } = await ajax("/api/sync", {
method: "POST",
body: JSON.stringify(request)
});
if (!success) {
toast.toast({
message: `<b>Failed to sync</b>\n${message}`,
type: "is-danger",
duration: 5000
});
}
} finally {
isLoading = false;
refresh();
Expand Down
5 changes: 4 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ export function ajax(
options?: RequestInit
): Promise<{ file: LedgerFile }>;

export function ajax(route: "/api/sync", options?: RequestInit): Promise<any>;
export function ajax(
route: "/api/sync",
options?: RequestInit
): Promise<{ success: boolean; message: string }>;
export function ajax(
route: "/api/price/providers",
options?: RequestInit
Expand Down

0 comments on commit 374b48e

Please sign in to comment.