Skip to content

Commit 269f723

Browse files
committed
Improve error management.
When there is an error, still log the end of the action + use right indentation.
1 parent 25119e0 commit 269f723

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/TinyLogger-Tests/TinyLoggerTest.class.st

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ No time : End: This is a new test
191191
stream close ]
192192
]
193193

194+
{ #category : #test }
195+
TinyLoggerTest >> testExecuteRecordedAsKeepRightIndentation [
196+
| contents stream |
197+
self skipInPharo6.
198+
logger
199+
timestampFormatBlock: [ :s | s nextPutAll: 'No time' ];
200+
removeAllLoggers;
201+
addStdoutLogger.
202+
stream := '' writeStream.
203+
[ Stdio stub stdout willReturn: stream.
204+
TinyCurrentLogger
205+
value: logger
206+
during: [ [ Object new execute: [ Error signal ] recordedAs: 'This is a new test' ]
207+
on: Error
208+
do: [ "nothing" ].
209+
Object new execute: [ 'test' record ] recordedAs: 'This is a new test' ].
210+
contents := Stdio stdout contents asString.
211+
"Ensure we have the right indentation."
212+
self
213+
assert: contents withUnixLineEndings
214+
equals:
215+
'No time : Begin: This is a new test
216+
No time : End with error: This is a new test
217+
No time : Begin: This is a new test
218+
No time : test
219+
No time : End: This is a new test
220+
' withUnixLineEndings ]
221+
ensure: [ Stdio recoverFromGHMutation.
222+
stream close ]
223+
]
224+
194225
{ #category : #test }
195226
TinyLoggerTest >> testFileLoggers [
196227
logger

src/TinyLogger/TinyLogger.class.st

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ TinyLogger >> execute: aBlock recordedAs: aString [
184184
self increaseDepthLevel.
185185
self record: 'Begin: ' , aString.
186186
self increaseDepthLevel.
187-
aBlock cull: aString.
187+
[ aBlock cull: aString ]
188+
ifCurtailed: [ "If there is an error, we ensure we keep the right indentation and we print an end of action with error."
189+
self decreaseDepthLevel.
190+
self record: 'End with error: ' , aString.
191+
self decreaseDepthLevel ].
188192
self decreaseDepthLevel.
189193
self record: 'End: ' , aString.
190194
self decreaseDepthLevel

0 commit comments

Comments
 (0)