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

HandlerNotFound should be published as Runtime.HandlerNotFound #113

Open
wants to merge 1 commit into
base: main
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
10 changes: 6 additions & 4 deletions src/Aws/Lambda/Runtime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ runLambda initializeCustomContext callback = do
-- Purposefully shadowing to prevent using the initial "empty" context
context <- Context.setEventData context event

( ( ( invokeAndRun callback manager lambdaApi event context
`Checked.catch` \err -> Publish.parsingError err lambdaApi context manager
( ( ( ( invokeAndRun callback manager lambdaApi event context
`Checked.catch` \err -> Publish.parsingError err lambdaApi context manager
)
`Checked.catch` \err -> Publish.invocationError err lambdaApi context manager
)
`Checked.catch` \err -> Publish.invocationError err lambdaApi context manager
`Checked.catch` \(err :: Error.EnvironmentVariableNotSet) -> Publish.runtimeInitError err lambdaApi context manager
)
`Checked.catch` \(err :: Error.EnvironmentVariableNotSet) -> Publish.runtimeInitError err lambdaApi context manager
`Unchecked.catch` \(err :: Error.HandlerNotFound) -> Publish.handlerNotFoundError err lambdaApi context manager
)
`Unchecked.catch` \err -> Publish.invocationError err lambdaApi context manager

Expand Down
11 changes: 11 additions & 0 deletions src/Aws/Lambda/Runtime/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Aws.Lambda.Runtime.Error
( EnvironmentVariableNotSet (..),
Parsing (..),
HandlerNotFound (..),
Invocation (..),
)
where
Expand Down Expand Up @@ -36,6 +37,16 @@ instance ToJSON Parsing where
"errorMessage" .= ("Could not parse '" <> valueName <> "': " <> errorMessage)
]

newtype HandlerNotFound = HandlerNotFound Text
deriving (Show, Exception)

instance ToJSON HandlerNotFound where
toJSON (HandlerNotFound handler) =
object
[ "errorType" .= ("Runtime.HandlerNotFound" :: Text),
"errorMessage" .= ("Could not find handler '" <> handler <> "'.")
]

newtype Invocation
= Invocation LBS.ByteString
deriving (Show, Exception)
9 changes: 9 additions & 0 deletions src/Aws/Lambda/Runtime/Publish.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Aws.Lambda.Runtime.Publish
( result,
invocationError,
parsingError,
handlerNotFoundError,
runtimeInitError,
)
where
Expand Down Expand Up @@ -56,6 +57,14 @@ parsingError err lambdaApi context =
(Endpoints.invocationError lambdaApi $ awsRequestId context)
context

-- | Publishes a HandlerNotFound error back to AWS Lambda
handlerNotFoundError :: Error.HandlerNotFound -> Text -> Context context -> Http.Manager -> IO ()
handlerNotFoundError err lambdaApi context =
publish
(encode err)
(Endpoints.invocationError lambdaApi $ awsRequestId context)
context

-- | Publishes a runtime initialization error back to AWS Lambda
runtimeInitError :: ToJSON err => err -> Text -> Context context -> Http.Manager -> IO ()
runtimeInitError err lambdaApi =
Expand Down
5 changes: 2 additions & 3 deletions src/Aws/Lambda/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import qualified Data.Text as Text
import Data.Typeable (Typeable)
import GHC.IO.Handle.FD (stderr)
import GHC.IO.Handle.Text (hPutStr)
import qualified Aws.Lambda.Runtime.Error as Error

type Handlers handlerType m context request response error =
HM.HashMap HandlerName (Handler handlerType m context request response error)
Expand Down Expand Up @@ -129,9 +130,7 @@ run dispatcherOptions mToIO handlers (LambdaOptions eventObject functionHandler
case HM.lookup functionHandler asIOCallbacks of
Just handlerToCall -> handlerToCall
Nothing ->
throwM $
userError $
"Could not find handler '" <> (Text.unpack . unHandlerName $ functionHandler) <> "'."
throwM $ Error.HandlerNotFound (unHandlerName functionHandler)

addStandaloneLambdaHandler ::
HandlerName ->
Expand Down