Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support annotated-exception #1762

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ packages:
- ./yesod
- ./yesod-eventsource
- ./yesod-websockets

extra-deps:
- annotated-exception-0.1.2.1
9 changes: 8 additions & 1 deletion stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
packages:
- completed:
hackage: annotated-exception-0.1.2.1@sha256:6bbc199b76c99aea3fa3c1a8c1272bd507f77704e04d42b39fa4b9d21bb702db,1738
pantry-tree:
size: 686
sha256: 1090170c2daa6fafe8a62513ec79583e20eda0df70edd0046488bc6c22aa066d
original:
hackage: annotated-exception-0.1.2.1
snapshots:
- completed:
size: 585603
Expand Down
4 changes: 4 additions & 0 deletions yesod-core/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for yesod-core

## 1.6.22.2

* Catches `AnnotatedException HandlerContents` so you can use `annotated-exception` with impunity in your web application code. [#1762](https://github.com/yesodweb/yesod/pull/1762)

## 1.6.22.1

+ Remove sometimes failing superfluous test. [#1756](https://github.com/yesodweb/yesod/pull/1756)
Expand Down
3 changes: 2 additions & 1 deletion yesod-core/src/Yesod/Core/Internal/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import qualified Control.Exception as EUnsafe
import Yesod.Core.Internal.Response
import Data.ByteString.Builder (toLazyByteString)
import qualified Data.ByteString.Lazy as BL
import Control.Exception.Annotated (AnnotatedException (..))
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Logger (LogLevel (LevelError), LogSource,
liftLoc)
Expand Down Expand Up @@ -116,7 +117,7 @@ basicRunHandler rhe handler yreq resState = do
return (HCContent defaultStatus tc))
(\e ->
case fromException e of
Just e' -> return e'
Just (AnnotatedException _ e') -> return e'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deserves a bit of commentary.

The instance Exception e => Exception (AnnotatedException e) has a slightly magical terrible horrifying unusual implementation of fromException.

The tests for this demonstrate the pattern, but tl;dr, the Exception instance here on AnnotatedException will work to catch both an AnnotatedException HandlerContents and a HandlerContents, which is then promoted to AnnotatedException mempty (hc :: HandlerContents).

Nothing -> HCError <$> toErrorHandler e)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, digging into this some more:

-- | Convert a synchronous exception into an ErrorResponse
toErrorHandler :: SomeException -> IO ErrorResponse
toErrorHandler e0 = handleAny errFromShow $
    case fromException e0 of
        Just (HCError x) -> evaluate $!! x
        _ -> errFromShow e0

So I think this function needs to also check for AnnotatedException - since the fromException e0 :: Maybe HCContents won't work if the underlying application code is calling throwWithCallStack.

-- | Generate an @ErrorResponse@ based on the shown version of the exception
errFromShow :: SomeException -> IO ErrorResponse
errFromShow x = do
  text <- evaluate (T.pack $ show x) `catchAny` \_ ->
          return (T.pack "Yesod.Core.Internal.Run.errFromShow: show of an exception threw an exception")
  return $ InternalError text

Fortunately this should work fine - perhaps we want to unwrap the AnnotatedException before showing it?


-- Get the raw state and return
Expand Down
3 changes: 2 additions & 1 deletion yesod-core/yesod-core.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yesod-core
version: 1.6.22.1
version: 1.6.22.2
license: MIT
license-file: LICENSE
author: Michael Snoyman <[email protected]>
Expand Down Expand Up @@ -27,6 +27,7 @@ library

build-depends: base >= 4.10 && < 5
, aeson >= 1.0
, annotated-exception >= 0.1.0.0
, auto-update
, blaze-html >= 0.5
, blaze-markup >= 0.7.1
Expand Down