diff --git a/library/Ratel.hs b/library/Ratel.hs index ccf6712..acb4df0 100644 --- a/library/Ratel.hs +++ b/library/Ratel.hs @@ -1,4 +1,5 @@ {-# LANGUAGE ImplicitParams #-} +{-# LANGUAGE OverloadedStrings #-} module Ratel where @@ -54,17 +55,17 @@ notify apiKey maybeManager initialPayload = do Left message -> fail message Right notice -> return (unwrapNoticeUuid (noticeUuid notice)) - -toError :: (?callStack :: Stack.CallStack) => Exception.SomeException -> Error +toError :: (Exception.Exception exception, Stack.HasCallStack) => exception -> Error toError exception = Error { errorBacktrace = Just (toTraces ?callStack) - , errorClass = Just (show (Typeable.typeOf exception)) + , errorClass = Just $ concat [ show (Typeable.typeOf exception) + , ": " + , (take 30 . takeWhile (/= '\n')) (Exception.displayException exception)] , errorMessage = Just (Exception.displayException exception) , errorSource = Nothing , errorTags = Nothing } - toTraces :: Stack.CallStack -> [Trace] toTraces callStack = map (uncurry toTrace) (Stack.getCallStack callStack) diff --git a/package.yaml b/package.yaml index b7dbdfa..45f4b19 100644 --- a/package.yaml +++ b/package.yaml @@ -36,4 +36,4 @@ tests: - tasty-hspec ==1.1.* main: Main.hs source-dirs: test-suite -version: '0.3.3' +version: '0.4.0' diff --git a/test-suite/RatelSpec.hs b/test-suite/RatelSpec.hs index 5e98e04..6097392 100644 --- a/test-suite/RatelSpec.hs +++ b/test-suite/RatelSpec.hs @@ -2,7 +2,7 @@ module RatelSpec (spec) where import qualified Control.Exception as Exception import qualified Ratel -import Test.Tasty.Hspec +import Test.Tasty.Hspec spec :: Spec spec = describe "Ratel" $ do @@ -22,7 +22,7 @@ spec = describe "Ratel" $ do , Ratel.traceNumber = Just "16:34" } ] - , Ratel.errorClass = Just "SomeException" + , Ratel.errorClass = Just "SomeException: something went wrong" , Ratel.errorMessage = Just "\ \something went wrong\n\ \CallStack (from HasCallStack):\n\ @@ -31,3 +31,27 @@ spec = describe "Ratel" $ do , Ratel.errorTags = Nothing } actual `shouldBe` expected) + it "should abbreviate longer errors" $ do + Exception.catch + (do + _ <- error "something went wrong, and now the server is on fire" + True `shouldBe` False) + (\ exception -> do + let actual = Ratel.toError (exception :: Exception.SomeException) + let expected = Ratel.Error + { Ratel.errorBacktrace = Just + [ Ratel.Trace + { Ratel.traceFile = Just "test-suite/RatelSpec.hs" + , Ratel.traceMethod = Just "RatelSpec.toError" + , Ratel.traceNumber = Just "40:34" + } + ] + , Ratel.errorClass = Just "SomeException: something went wrong, and now " + , Ratel.errorMessage = Just "\ + \something went wrong, and now the server is on fire\n\ + \CallStack (from HasCallStack):\n\ + \ error, called at test-suite/RatelSpec.hs:37:26 in main:RatelSpec" + , Ratel.errorSource = Nothing + , Ratel.errorTags = Nothing + } + actual `shouldBe` expected)