Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions library/Ratel.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE OverloadedStrings #-}

module Ratel where

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ tests:
- tasty-hspec ==1.1.*
main: Main.hs
source-dirs: test-suite
version: '0.3.3'
version: '0.4.0'
28 changes: 26 additions & 2 deletions test-suite/RatelSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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\
Expand All @@ -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)