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

Fix error handling in pure runtimes #130

Merged
merged 8 commits into from
Dec 19, 2023
Merged
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
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog][chg] and this project adheres to
[Haskell's Package Versioning Policy][pvp]

## 1.0.1 - 2023-12-15
## unreleased - 2023-11-23

- `fallibleRuntime` and `fallibleRuntimeWithContext` report errors to AWS
instead of returning `{"Left": "some error"}`.
IamfromSpace marked this conversation as resolved.
Show resolved Hide resolved
- `fallibleRuntime` and `fallibleRuntimeWithContext` no longer wrap their
successful responses in `{"Right": ...}`.
- `pureRuntime`, `pureRuntimeWithContext`, `fallibleRuntime` and `fallibleRuntimeWithContext`
no longer crash the Lambda runtime if input can't be parsed from JSON.

## `1.0.1` - 2023-12-15

- Add support for aeson 2.2. Users can opt in by setting the `use-aeson-2-2` flag to `true`, which for many users will be set automatically. In a future breaking change release, this flag will default to `true`.

Expand Down
7 changes: 6 additions & 1 deletion src/AWS/Lambda/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ They map functions (instead of values) to turn basic handlers into handlers comp

module AWS.Lambda.Combinators (
withoutContext,
withInfallibleParse
withInfallibleParse,
withFallibleParse
) where

import Data.Aeson (FromJSON, parseJSON, Value)
Expand Down Expand Up @@ -70,3 +71,7 @@ withoutContext = const
-- on a failure to convert the JSON AST sent to the Lambda.
withInfallibleParse :: FromJSON a => (a -> b) -> Value -> b
withInfallibleParse fn = either error fn . parseEither parseJSON

-- | Like 'withInfallibleParse', but return the result in an 'Either' instead of using 'error'.
withFallibleParse :: FromJSON a => (a -> b) -> Value -> Either String b
withFallibleParse fn = fmap fn . parseEither parseJSON
2 changes: 1 addition & 1 deletion src/AWS/Lambda/Context.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE NamedFieldPuns, OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}

{-|
Module : AWS.Lambda.Context
Expand Down
13 changes: 8 additions & 5 deletions src/AWS/Lambda/Runtime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ module AWS.Lambda.Runtime (
mRuntimeWithContext
) where

import AWS.Lambda.Combinators (withInfallibleParse)
import AWS.Lambda.Combinators (withInfallibleParse,
withFallibleParse,
withoutContext)
import AWS.Lambda.Context (LambdaContext(..))
import qualified AWS.Lambda.Runtime.Value as ValueRuntime
import Control.Monad (join)
import Control.Monad.Catch (MonadCatch)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Reader (ReaderT)
Expand Down Expand Up @@ -253,7 +256,7 @@ ioRuntime = ValueRuntime.ioRuntime . withInfallibleParse
-- @
fallibleRuntimeWithContext :: (FromJSON event, ToJSON result) =>
(LambdaContext -> event -> Either String result) -> IO ()
fallibleRuntimeWithContext = ValueRuntime.fallibleRuntimeWithContext . fmap withInfallibleParse
fallibleRuntimeWithContext fn = ValueRuntime.fallibleRuntimeWithContext $ \lc -> join . withFallibleParse (fn lc)

-- | For pure functions that can still fail.
--
Expand Down Expand Up @@ -286,7 +289,7 @@ fallibleRuntimeWithContext = ValueRuntime.fallibleRuntimeWithContext . fmap with
-- @
fallibleRuntime :: (FromJSON event, ToJSON result) =>
(event -> Either String result) -> IO ()
fallibleRuntime = ValueRuntime.fallibleRuntime . withInfallibleParse
fallibleRuntime = fallibleRuntimeWithContext . withoutContext

-- | For pure functions that can never fail that also need access to the context.
--
Expand Down Expand Up @@ -318,7 +321,7 @@ fallibleRuntime = ValueRuntime.fallibleRuntime . withInfallibleParse
-- @
pureRuntimeWithContext :: (FromJSON event, ToJSON result) =>
(LambdaContext -> event -> result) -> IO ()
pureRuntimeWithContext = ValueRuntime.pureRuntimeWithContext . fmap withInfallibleParse
pureRuntimeWithContext = fallibleRuntimeWithContext . fmap (fmap pure)

-- | For pure functions that can never fail.
--
Expand All @@ -345,4 +348,4 @@ pureRuntimeWithContext = ValueRuntime.pureRuntimeWithContext . fmap withInfallib
-- main = pureRuntime myHandler
-- @
pureRuntime :: (FromJSON event, ToJSON result) => (event -> result) -> IO ()
pureRuntime = ValueRuntime.pureRuntime . withInfallibleParse
pureRuntime = pureRuntimeWithContext . withoutContext
7 changes: 4 additions & 3 deletions src/AWS/Lambda/Runtime/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ module AWS.Lambda.Runtime.Value (
mRuntimeWithContext,
) where

import AWS.Lambda.RuntimeClient (RuntimeClientConfig, getRuntimeClientConfig,
getNextData, sendEventError, sendEventSuccess)
import AWS.Lambda.Combinators (withoutContext)
import AWS.Lambda.Context (LambdaContext(..))
import AWS.Lambda.RuntimeClient (RuntimeClientConfig, getNextData,
getRuntimeClientConfig,
sendEventError, sendEventSuccess)
import Control.Exception (SomeException, displayException)
import Control.Monad ((<=<), forever)
import Control.Monad.Catch (MonadCatch, try)
Expand Down Expand Up @@ -331,7 +332,7 @@ ioRuntime = ioRuntimeWithContext . withoutContext
-- @
fallibleRuntimeWithContext :: ToJSON result =>
(LambdaContext -> Value -> Either String result) -> IO ()
fallibleRuntimeWithContext = mRuntimeWithContext . fmap (fmap pure)
fallibleRuntimeWithContext fn = mRuntimeWithContext (\lc -> either error pure . fn lc)
kokobd marked this conversation as resolved.
Show resolved Hide resolved

-- | For pure functions that can still fail.
--
Expand Down
Loading