Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bracket: How to ignore exceptions from the "release resource" function?

The current behaviour of bracket causes the following expression to throw inside release. However, this is behaviour doesn't work for my use-case.

Which standard Haskell function would throw the original exception raised by the core action, i.e. it will throw inside action?


bracket 
  (pure ()) -- acquire resource
  (const $ UnliftIO.throwString "inside release") -- release resource
  (const $ UnliftIO.throwString "inside action") -- action which uses the resource

Why do I need this? In my case the "resource" is running an SQL statement which will effect the SQL connection as long as the session exists. I want to make sure that the effect of that SQL statement is always undone via "release resource". However, if the "core action" itself has an SQL error, then "release resource" always fails with current transaction is aborted, commands ignored until end of transaction block which completely gobbles-up the core/underlying exception thrown by the action.

like image 334
Saurabh Nanda Avatar asked Mar 24 '26 01:03

Saurabh Nanda


1 Answers

It seems to me you could just catch the error inside the release function:

bracket 
  (pure ())
  (const $
    catch 
      (UnliftIO.throwString "inside release") 
      (\(_ :: StringException) -> pure ()))
  (const $ UnliftIO.throwString "inside action")
like image 54
Noughtmare Avatar answered Mar 25 '26 20:03

Noughtmare