Add handling for ledger errors when converting to Org tables

This commit is contained in:
Daniel Borchmann 2024-07-06 12:03:46 +02:00
parent fb6bb224cb
commit 2318ffa199
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -1086,27 +1086,38 @@ where the rows are all accounts contained in the result of
COMMAND, and where the columns are the individual transactions, COMMAND, and where the columns are the individual transactions,
headlined with their date." headlined with their date."
;; XXX: this implementation is certainly not ideal (let* (ledger-output
(let* (transactions dates accounts result) ledger-output-parsed
transactions
dates
accounts
result)
;; Call command and catch result (condition-case err
(setq transactions (condition-case err (setq ledger-output (shell-command-to-string command))
(car (read-from-string (shell-command-to-string command))) ((error debug) (error "Failed to run ledger command: %s" err)))
((error debug) (error "Failed to run ledger command: %s" err))))
(setq ledger-output-parsed (read-from-string ledger-output))
;; Ensure we have parsed the complete output, i.e., `ledger-output' is a lisp form.
(unless (= (1- (length ledger-output))
(cdr ledger-output-parsed))
(error "Failed to parse ledger output\nRead: %s\nGot: %s" ledger-output-parsed ledger-output))
;; Format time and remove unnecessary transactions ;; Format time and remove unnecessary transactions
(setq transactions (-map #'(lambda (row) (setq transactions (-map #'(lambda (row)
(list (format-time-string "%F" (nth 2 row)) (list (format-time-string "%F" (nth 2 row))
(-map #'(lambda (entry) (-map #'(lambda (entry)
(list (nth 1 entry) (list (nth 1 entry)
(nth 2 entry))) (nth 2 entry)))
(-drop 5 row)))) (-drop 5 row))))
transactions)) (car ledger-output-parsed)))
(setq dates (-map #'-first-item transactions)) (setq dates (-map #'-first-item transactions))
;; Transfer list output into alist for later direct access; `accounts' will map accounts to alists ;; Transfer list output into alist for later direct access; `accounts' will map accounts to
;; mapping dates to values ;; alists mapping dates to values
;; FIXME: this implementation is certainly not ideal
(dolist (row transactions) (dolist (row transactions)
(let ((date (-first-item row))) (let ((date (-first-item row)))
(dolist (entry (-second-item row)) (dolist (entry (-second-item row))