diff --git a/fs-sim/CHANGELOG.md b/fs-sim/CHANGELOG.md index 6991adc..3029283 100644 --- a/fs-sim/CHANGELOG.md +++ b/fs-sim/CHANGELOG.md @@ -1,5 +1,12 @@ # Revision history for fs-sim +## ?.?.?.? -- ????-??-?? + +### Patch + +* Fix a bug where `withErrors` would not put back the previous `Errors` when an + exception is thrown during execution of the function. + ## 0.3.1.0 -- 2024-12-10 ### Non-breaking diff --git a/fs-sim/src/System/FS/Sim/Error.hs b/fs-sim/src/System/FS/Sim/Error.hs index f55b2e6..5897ad8 100644 --- a/fs-sim/src/System/FS/Sim/Error.hs +++ b/fs-sim/src/System/FS/Sim/Error.hs @@ -572,15 +572,12 @@ runSimErrorFS mockFS errors action = do -- | Execute the next action using the given 'Errors'. After the action is -- finished, the previous 'Errors' are restored. -withErrors :: MonadSTM m => StrictTVar m Errors -> Errors -> m a -> m a -withErrors errorsVar tempErrors action = do - originalErrors <- atomically $ do - originalErrors <- readTVar errorsVar - writeTVar errorsVar tempErrors - return originalErrors - res <- action - atomically $ writeTVar errorsVar originalErrors - return res +withErrors :: (MonadSTM m, MonadThrow m) => StrictTVar m Errors -> Errors -> m a -> m a +withErrors errorsVar tempErrors action = + bracket + (atomically $ swapTVar errorsVar tempErrors) + (\originalErrors -> atomically $ swapTVar errorsVar originalErrors) + $ \_ -> action {------------------------------------------------------------------------------- Utilities