Skip to content

Commit 31d3649

Browse files
committed
capture exception value for internal errors
1 parent c9f7ee0 commit 31d3649

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

yesod-core/ChangeLog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# ChangeLog for yesod-core
22

3+
## 1.6.28.0
4+
5+
* Capture exception value in internal errors [#1860](https://github.com/yesodweb/yesod/pull/1869)
6+
7+
## 1.6.27.1
8+
9+
* Set `base >= 4.11` for less CPP and imports [#1876](https://github.com/yesodweb/yesod/pull/1876)
10+
311
## 1.6.27.0
412

513
* Build with `wai-extra-3.1.17` [#1861](https://github.com/yesodweb/yesod/pull/1861)

yesod-core/src/Yesod/Core/Class/Yesod.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,13 +702,14 @@ defaultErrorHandler (InvalidArgs ia) = selectRep $ do
702702
provideRep $ return ("Invalid Arguments: " <> T.intercalate " " ia)
703703

704704
defaultErrorHandler (InternalError e) = do
705-
$logErrorS "yesod-core" e
705+
let exceptionString = displayException e
706+
$logErrorS "yesod-core" exceptionString
706707
selectRep $ do
707708
provideRep $ defaultLayout $ defaultMessageWidget
708709
"Internal Server Error"
709-
[hamlet|<pre>#{e}|]
710-
provideRep $ return $ object ["message" .= ("Internal Server Error" :: Text), "error" .= e]
711-
provideRep $ return $ "Internal Server Error: " <> e
710+
[hamlet|<pre>#{exceptionString}|]
711+
provideRep $ return $ object ["message" .= ("Internal Server Error" :: Text), "error" .= exceptionString]
712+
provideRep $ return $ "Internal Server Error: " <> exceptionString
712713

713714
defaultErrorHandler (BadMethod m) = selectRep $ do
714715
provideRep $ defaultLayout $ defaultMessageWidget

yesod-core/src/Yesod/Core/Internal/Response.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ evaluateContent (ContentBuilder b mlen) = handle f $ do
101101
len `seq` return (Right $ ContentBuilder (lazyByteString lbs) mlen')
102102
where
103103
f :: SomeException -> IO (Either ErrorResponse Content)
104-
f = return . Left . InternalError . T.pack . show
104+
f = return . Left . InternalError
105105
evaluateContent c = return (Right c)
106106

107107
getStatus :: ErrorResponse -> H.Status

yesod-core/src/Yesod/Core/Internal/Run.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{-# LANGUAGE ScopedTypeVariables #-}
99
module Yesod.Core.Internal.Run
1010
( toErrorHandler
11-
, errFromShow
11+
, errFromDisplayException
1212
, basicRunHandler
1313
, handleError
1414
, handleContents
@@ -59,17 +59,10 @@ import Data.Proxy(Proxy(..))
5959

6060
-- | Convert a synchronous exception into an ErrorResponse
6161
toErrorHandler :: SomeException -> IO ErrorResponse
62-
toErrorHandler e0 = handleAny errFromShow $
62+
toErrorHandler e0 = handleAny errFromDisplayException $
6363
case fromException e0 of
6464
Just (HCError x) -> evaluate $!! x
65-
_ -> errFromShow e0
66-
67-
-- | Generate an @ErrorResponse@ based on the shown version of the exception
68-
errFromShow :: SomeException -> IO ErrorResponse
69-
errFromShow x = do
70-
text <- evaluate (T.pack $ show x) `catchAny` \_ ->
71-
return (T.pack "Yesod.Core.Internal.Run.errFromShow: show of an exception threw an exception")
72-
return $ InternalError text
65+
_ -> InternalError e0
7366

7467
-- | Do a basic run of a handler, getting some contents and the final
7568
-- @GHState@. The @GHState@ unfortunately may contain some impure
@@ -128,7 +121,7 @@ handleError :: RunHandlerEnv sub site
128121
-> IO YesodResponse
129122
handleError rhe yreq resState finalSession headers e0 = do
130123
-- Find any evil hidden impure exceptions
131-
e <- (evaluate $!! e0) `catchAny` errFromShow
124+
e <- (evaluate $!! e0) `catchAny` errFromDisplayException
132125

133126
-- Generate a response, leveraging the updated session and
134127
-- response headers
@@ -263,7 +256,7 @@ runFakeHandler :: forall site m a . (Yesod site, MonadIO m) =>
263256
-> HandlerFor site a
264257
-> m (Either ErrorResponse a)
265258
runFakeHandler fakeSessionMap logger site handler = liftIO $ do
266-
ret <- I.newIORef (Left $ InternalError "runFakeHandler: no result")
259+
ret <- I.newIORef (Left $ InternalError $ toException (EUnsafe.ErrorCall "runFakeHandler: no result"))
267260
maxExpires <- getCurrentMaxExpiresRFC1123
268261
let handler' = liftIO . I.writeIORef ret . Right =<< handler
269262
let yapp = runHandler

yesod-core/src/Yesod/Core/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ data ErrorResponse =
342342
-- ^ The requested resource was not found.
343343
-- Examples of when this occurs include when an incorrect URL is used, or @yesod-persistent@'s 'get404' doesn't find a value.
344344
-- HTTP status: 404.
345-
| InternalError !Text
345+
| InternalError !SomeException
346346
-- ^ Some sort of unexpected exception.
347347
-- If your application uses `throwIO` or `error` to throw an exception, this is the form it would take.
348348
-- HTTP status: 500.

yesod-core/yesod-core.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: yesod-core
2-
version: 1.6.27.0
2+
version: 1.6.27.1
33
license: MIT
44
license-file: LICENSE
55
author: Michael Snoyman <michael@snoyman.com>

0 commit comments

Comments
 (0)