raven-haskell-scotty (empty) → 0.1.0.0
raw patch · 4 files changed
+133/−0 lines, 4 filesdep +basedep +bytestringdep +case-insensitivesetup-changed
Dependencies added: base, bytestring, case-insensitive, mtl, raven-haskell, scotty, text, wai
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- raven-haskell-scotty.cabal +27/−0
- src/System/Log/Raven/Scotty.hs +84/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2013 Alexander Bondarenko++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ raven-haskell-scotty.cabal view
@@ -0,0 +1,27 @@+-- Initial raven-haskell-scotty.cabal generated by cabal init. For further+-- documentation, see http://haskell.org/cabal/users-guide/++name: raven-haskell-scotty+version: 0.1.0.0+synopsis: Http interface for Scotty web server.+description: Utilities to log errors in Scotty actions using raven-haskell.+homepage: http://bitbucket.org/dpwiz/raven-haskell-scotty+license: MIT+license-file: LICENSE+author: Alexander Bondarenko+maintainer: aenor.realm@gmail.com+-- copyright: +category: Web+build-type: Simple+cabal-version: >=1.8++library+ hs-source-dirs: src/+ exposed-modules:+ System.Log.Raven.Scotty+ build-depends:+ base ==4.*,+ raven-haskell,+ scotty, wai, case-insensitive,+ bytestring, text,+ mtl
+ src/System/Log/Raven/Scotty.hs view
@@ -0,0 +1,84 @@+{-# LANGUAGE ScopedTypeVariables #-}++-- | This package contains utilities to log errors in Scotty actions using raven-haskell.++module System.Log.Raven.Scotty+ ( guardIO+ , logError+ , scottyHttpInterface+ ) where++import Web.Scotty (ActionM, request, reqHeader, params)++import System.Log.Raven as Raven+import System.Log.Raven.Types as Raven+import System.Log.Raven.Transport.HttpConduit (sendRecord)+import qualified System.Log.Raven.Interfaces as SI+import Network.Wai(Request(..))++import qualified Data.ByteString.Char8 as BS+import qualified Data.Text.Lazy as TL+import qualified Data.CaseInsensitive as CI++import Control.Monad.Trans (liftIO)+import Control.Exception (try, throw, SomeException)++-- | A liftIO alternative that logs unhandled exceptions.+-- The function itself is verbose in arguments and designed to be curried and reused.+--+-- > main = do+-- > raven <- initRaven …+-- > let hereBeDragons = guardIO raven "my.logger" (Just "DragonsError") (Just "My.Inner.Dragons")+-- >+-- > scotty 8000 $ do+-- > post "/some/action/" $ do+-- > arg1 <- param "arg1"+-- > arg2 <- param "arg2"+-- > hereBeDragons $ dragonsIO arg1 arg2+guardIO :: SentryService -- ^ Configured Sentry service.+ -> String -- ^ Logger name.+ -> Maybe String -- ^ Exception type name.+ -> Maybe String -- ^ Action module name.+ -> IO a -- ^ Action to run.+ -> ActionM a -- ^ Result in a Scotty ActionM monad.+guardIO raven logger typename modname io = do+ res <- liftIO $ try io+ case res of+ Right r -> return r+ Left (e :: SomeException) -> do+ let exc = SI.exception (show e) typename modname+ logError raven logger (show e) exc+ liftIO $ throw e++-- | Log an error in an ActionM monad, collecting request data.+logError :: SentryService -- ^ A configured Sentry service.+ -> String -- ^ Logger name.+ -> String -- ^ Message to log.+ -> (SentryRecord -> SentryRecord) -- ^ Additional interfaces or other updates.+ -> ActionM ()+logError raven logger msg upd = do+ ifHttp <- scottyHttpInterface+ liftIO $ register raven logger Error msg (upd . ifHttp)++-- | Collect request parameters for a HTTP sentry interface.+scottyHttpInterface :: ActionM (SentryRecord -> SentryRecord)+scottyHttpInterface = do+ r <- request+ let method = BS.unpack $ requestMethod r+ let qs = case BS.unpack $ rawQueryString r of+ "" -> Nothing+ c -> Just c+ let hs = [ (BS.unpack . CI.original $ h, BS.unpack v)+ | (h, v) <- requestHeaders r+ ]++ host <- reqHeader (TL.pack "Host")+ let url = "http://" ++ TL.unpack host ++ BS.unpack (rawPathInfo r)++ ps <- params+ let args = SI.QueryArgs [ (TL.unpack k, TL.unpack v)+ | (k, v) <- ps+ ]++ liftIO $ print (url, method, args, qs, hs)+ return $ SI.http url method args qs Nothing hs []