packages feed

rollbar-yesod (empty) → 0.1.0

raw patch · 9 files changed

+313/−0 lines, 9 filesdep +basedep +hspecdep +rollbar-clientsetup-changed

Dependencies added: base, hspec, rollbar-client, rollbar-wai, rollbar-yesod, unliftio, wai, warp, yesod-core, yesod-test

Files

+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for rollbar-yesod++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,19 @@+Copyright 2020 Stack Builders Inc.++Permission is hereby granted, free of charge, to any person obtaining a copy of+this software and associated documentation files (the "Software"), to deal in+the Software without restriction, including without limitation the rights to+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies+of the Software, and to permit persons to whom the Software is furnished to do+so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,12 @@+# Rollbar Yesod++Provides error reporting capabilities to [Yesod](https://www.yesodweb.com/)+applications through [Rollbar API](https://explorer.docs.rollbar.com/).++## Getting Started++Read the instructions [here](../README.md).++## Example++For a complete example check the link [here](example/Main.hs).
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ example/Main.hs view
@@ -0,0 +1,36 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}++module Main+  ( main+  ) where++import Network.Wai.Handler.Warp (run)+import Rollbar.Client+import Rollbar.Yesod (rollbarYesodMiddleware)+import Yesod.Core++newtype App = App { appRollbarSettings :: Settings }++mkYesod "App" [parseRoutes|+  / RootR GET+|]++instance HasSettings Handler where+  getSettings = getsYesod appRollbarSettings++instance Yesod App where+  yesodMiddleware = rollbarYesodMiddleware . defaultYesodMiddleware++getRootR :: Handler ()+getRootR = error "yesod"++main :: IO ()+main = do+  settings <- readSettings "rollbar.yaml"+  toWaiApp (App settings) >>= run 3000
+ rollbar-yesod.cabal view
@@ -0,0 +1,92 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: d007a8f11fac3e9745d78838751cf81ae9ce6f553d2a8e7f8aad77b97ad6d903++name:           rollbar-yesod+version:        0.1.0+synopsis:       Provides error reporting capabilities to Yesod applications through Rollbar API.++description:    Please see the README on GitHub at+                <https://github.com/stackbuilders/rollbar-haskell/tree/master/rollbar-yesod>+homepage:       https://github.com/stackbuilders/rollbar-haskell#readme+bug-reports:    https://github.com/stackbuilders/rollbar-haskell/issues+author:         Stack Builders Inc.+maintainer:     Sebastián Estrella <sestrella@stackbuilders.com>+copyright:      2020 Stack Builders Inc.+license:        MIT+license-file:   LICENSE+tested-with:    GHC ==8.6.5, GHC ==8.8.4, GHC ==8.10.2+build-type:     Simple+extra-source-files:+    README.md+    ChangeLog.md++source-repository head+  type: git+  location: https://github.com/stackbuilders/rollbar-haskell++flag example+  description: Build the example+  manual: False+  default: False++library+  exposed-modules:+      Rollbar.Yesod+  other-modules:+      Paths_rollbar_yesod+  hs-source-dirs:+      src+  ghc-options: -Wall+  build-depends:+      base >=4.12 && <5+    , rollbar-client >=0.1 && <1+    , rollbar-wai >=0.1 && <1+    , unliftio >=0.2 && <1+    , wai >=3.2 && <4+    , yesod-core >=1.6 && <2+  default-language: Haskell2010++executable yesod-example+  main-is: Main.hs+  other-modules:+      Paths_rollbar_yesod+  hs-source-dirs:+      example+  ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N+  build-depends:+      base >=4.12 && <5+    , rollbar-client+    , rollbar-yesod+    , warp >=3.3 && <4+    , yesod-core+  if flag(example)+    buildable: True+  else+    buildable: False+  default-language: Haskell2010++test-suite spec+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Rollbar.YesodSpec+      Paths_rollbar_yesod+  hs-source-dirs:+      test+  ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N+  build-tool-depends:+      hspec-discover:hspec-discover >=2.7 && <3+  build-depends:+      base >=4.12 && <5+    , hspec >=2.7 && <3+    , rollbar-client+    , rollbar-yesod+    , wai+    , yesod-core+    , yesod-test >=1.6 && <2+  default-language: Haskell2010
+ src/Rollbar/Yesod.hs view
@@ -0,0 +1,68 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE OverloadedStrings #-}++-- |+-- Module: Rollbar.Yesod+-- Copyright: (c) 2020 Stack Builders Inc.+-- License: MIT+-- Maintainer: Sebastián Estrella <sestrella@stackbuilders.com>+--+-- For a fully working example check the following link:+--+-- <https://github.com/stackbuilders/rollbar-haskell/blob/master/rollbar-yesod/example/Main.hs>+module Rollbar.Yesod+  ( rollbarYesodMiddleware+  , rollbarYesodMiddlewareWith+  ) where++import qualified Network.Wai as W++import Control.Exception (Exception(..), SomeException)+import Control.Monad (unless, void)+import Rollbar.Client+import Rollbar.Wai (rollbarOnExceptionWith)+import UnliftIO.Exception (catch, throwIO)+import Yesod.Core+import Yesod.Core.Types (HandlerContents)++-- | Captures non 'HandlerContents' exceptions and send them to Rollbar via the+-- API. Under the hood, this function uses 'createItem' function from+-- rollbar-client.+--+-- __Example__+--+-- > instance Yesod App where+-- >   yesodMiddleware = rollbarYesodMiddleware . defaultYesodMiddleware+--+-- @since 0.1.0+rollbarYesodMiddleware+  :: (HasSettings m, MonadHandler m, MonadUnliftIO m)+  => m a+  -> m a+rollbarYesodMiddleware = rollbarYesodMiddlewareWith $ \settings request ex ->+  rollbarOnExceptionWith handler settings (Just request) ex+  where+    handler item = void $ createItem item { itemFramework = Just "yesod" }++-- | Similar to 'rollbarYesodMiddleware', but it allows customize the function+-- used to send the 'SomeException' captured from a handler to Rollbar.+--+-- @since 0.1.0+rollbarYesodMiddlewareWith+  :: (HasSettings m, MonadHandler m, MonadUnliftIO m)+  => (Settings -> W.Request -> SomeException -> m ())+  -> m a+  -> m a+rollbarYesodMiddlewareWith f handler = handler `catch` \ex -> do+  unless (isHandlerContents ex) $ do+    settings <- getSettings+    wrequest <- waiRequest+    f settings wrequest ex++  throwIO ex++isHandlerContents :: SomeException -> Bool+isHandlerContents = maybe False handlerContents . fromException+  where+    handlerContents :: HandlerContents -> Bool+    handlerContents = const True
+ test/Rollbar/YesodSpec.hs view
@@ -0,0 +1,80 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# OPTIONS_GHC -fno-warn-unused-top-binds #-}++module Rollbar.YesodSpec+  ( spec+  ) where++import qualified Network.Wai as W++import Data.IORef+import Data.Maybe+import Rollbar.Client+import Rollbar.Yesod+import Test.Hspec+import Yesod.Core+import Yesod.Test++data App = App+  { appRollbarSettings :: Settings+  , appRequestRef :: IORef (Maybe W.Request)+  }++mkYesod "App" [parseRoutes|+  /error ErrorR GET+  /success SuccessR GET+|]++instance HasSettings Handler where+  getSettings = getsYesod appRollbarSettings++instance Yesod App where+  yesodMiddleware handler = do+    requestRef <- getsYesod appRequestRef+    rollbarYesodMiddlewareWith (writeRequest requestRef) $+      defaultYesodMiddleware handler+    where+      writeRequest requestRef _ wrequest _ =+        liftIO $ writeIORef requestRef $ Just wrequest++getErrorR :: Handler ()+getErrorR = error "Boom"++getSuccessR :: Handler ()+getSuccessR = return ()++spec :: Spec+spec = withApp $+  describe "rollbarYesodMiddlewareWith" $ do+    context "when there is an error" $+      it "triggers a call to Rollbar" $ do+        get ErrorR+        statusIs 500+        requestRef <- appRequestRef <$> getTestYesod+        liftIO $ do+          wrequest <- readIORef requestRef+          wrequest `shouldSatisfy` isJust++    context "when there is no error" $+      it "does not trigger a call to Rollbar" $ do+        get SuccessR+        statusIs 200+        requestRef <- appRequestRef <$> getTestYesod+        liftIO $ do+          wrequest <- readIORef requestRef+          wrequest `shouldSatisfy` isNothing+++withApp :: SpecWith (TestApp App) -> Spec+withApp = before $ do+  app <- getApp+  return (app, id)+  where+    getApp =+      App <$> readSettings "rollbar.yaml"+          <*> newIORef Nothing
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}