<commit_before>{-# LANGUAGE ScopedTypeVariables #-}

module Propellor.Exception where

import Propellor.Types
import Propellor.Types.Exception
import Propellor.Message
import Utility.Exception

import Control.Exception (AsyncException)
import Control.Monad.Catch
import Control.Monad.IO.Class (MonadIO)
import Control.Applicative

-- | Catches all exceptions (except for `StopPropellorException` and
-- `AsyncException`) and returns FailedChange.
catchPropellor :: (MonadIO m, MonadCatch m) => m Result -> m Result
catchPropellor a = either err return =<< tryPropellor a
  where
	err e =  warningMessage (show e) >> return FailedChange

catchPropellor' :: MonadCatch m => m a -> (SomeException -> m a) -> m a
catchPropellor' a onerr = a `catches`
	[ Handler (\ (e :: AsyncException) -> throwM e)
	, Handler (\ (e :: StopPropellorException) -> throwM e)
	, Handler (\ (e :: SomeException) -> onerr e)
	]

-- | Catches all exceptions (except for `StopPropellorException` and
-- `AsyncException`).<commit_msg>Exception: import Prelude after Control.Applicative to prevent warning<commit_after>{-# LANGUAGE ScopedTypeVariables #-}

module Propellor.Exception where

import Propellor.Types
import Propellor.Types.Exception
import Propellor.Message
import Utility.Exception

import Control.Exception (AsyncException)
import Control.Monad.Catch
import Control.Monad.IO.Class (MonadIO)
import Control.Applicative
import Prelude

-- | Catches all exceptions (except for `StopPropellorException` and
-- `AsyncException`) and returns FailedChange.
catchPropellor :: (MonadIO m, MonadCatch m) => m Result -> m Result
catchPropellor a = either err return =<< tryPropellor a
  where
	err e =  warningMessage (show e) >> return FailedChange

catchPropellor' :: MonadCatch m => m a -> (SomeException -> m a) -> m a
catchPropellor' a onerr = a `catches`
	[ Handler (\ (e :: AsyncException) -> throwM e)
	, Handler (\ (e :: StopPropellorException) -> throwM e)
	, Handler (\ (e :: SomeException) -> onerr e)
	]

-- | Catches all exceptions (except for `StopPropellorException` and
-- `AsyncException`).
{'fraction_non_alphanumeric': 0.11228406909788867, 'fraction_numerical': 0.0, 'mean_word_length': 4.6811989100817435, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2854172', 'n_tokens_mistral': 656, 'n_tokens_neox': 643, 'n_words': 202}