Skip to content

Commit 6cefce9

Browse files
committed
Log errors when executing balance command
1 parent 8e4e8eb commit 6cefce9

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file on its own is not valid,
2+
# but it can be merged with other journals
3+
transactions:
4+
- utc: '2024-05-28 14:35'
5+
note: Bread and butter
6+
transfers:
7+
- from: john:giro
8+
to: non_existant_bakery
9+
amount: 4.19 €

makefile

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ test-cli: | node_modules
8686
balance examples/journal.yaml examples/journal-only-transactions.yaml \
8787
> /dev/null
8888

89+
# Following command should fail
90+
@bun x spago run -- \
91+
balance examples/journal.yaml examples/journal-broken-transaction.yaml \
92+
&& echo "❌ This must fail" && exit 1 \
93+
|| echo "✅ Balance printed an error"
94+
8995
bun x spago run -- \
9096
unused-files examples/receipts examples/journal.yaml \
9197
2> /dev/null

src/Main.purs

+1-13
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,6 @@ buildLedgerAndRun currentDir journalPathRel extraJournalPaths callback = do
205205
Error msg -> pure $ Error msg
206206
Ok ledger -> ledger # callback
207207

208-
buildRunExit
209-
:: String
210-
-> String
211-
-> Array CliArgPrim
212-
-> (Ledger -> Effect (Result String Unit))
213-
-> Effect (Result String Unit)
214-
buildRunExit currentDir journalPathRel extraJournalPaths callback = do
215-
buildLedgerAndRun currentDir journalPathRel extraJournalPaths callback
216-
>>= \res -> case res of
217-
Ok val -> pure $ Ok val
218-
Error msg -> errorAndExit config msg
219-
220208
getAllFiles :: String -> Effect (Array String)
221209
getAllFiles directoryPath =
222210
let
@@ -247,7 +235,7 @@ checkUnusedFiles
247235
:: String -> String -> Array CliArgPrim -> Effect (Result String Unit)
248236
checkUnusedFiles filesDirPath jourPathRel extraJournalPaths = do
249237
currentDir <- cwd
250-
buildRunExit currentDir jourPathRel extraJournalPaths $
238+
buildLedgerAndRun currentDir jourPathRel extraJournalPaths $
251239
\ledger@(Ledger { transactions }) -> do
252240
let
253241
journalDir =

src/Oclis/Executor.purs

+8-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,14 @@ callCliAppWithOutput doesPrint executor =
357357
setExitCode 1
358358
Ok cliSpec -> do
359359
arguments <- argv
360-
_ <- callCliAppWith cliSpec executor doesPrint arguments
360+
result <- callCliAppWith cliSpec executor doesPrint arguments
361+
case result of
362+
Error errMsg -> do
363+
error (makeRed errMsg)
364+
setExitCode 1
365+
Ok val -> do
366+
log val
367+
setExitCode 0
361368
pure unit
362369

363370
callCliAppWith

0 commit comments

Comments
 (0)