servant-0.1: A library to generate REST-style webservices on top of scotty, handling all the boilerplate for you

Stabilityexperimental
MaintainerAlp Mestanogullari <alp@zalora.com>
Safe HaskellSafe-Inferred

Servant.Error

Contents

Description

Everything you'll need when doing some kind of exception handling around your "database operations".

Synopsis

Creating an combining ExceptionCatchers

data ExceptionCatcher err Source

A container for zero or more exception catchers.

By exception catcher, we mean here a function that can convert some precise instance of Exception to the error type used in your scotty app (the e type in Servant.Resource Servant.Service)

Instances

Monoid (ExceptionCatcher err)

If you're into Monoids, enjoy our mempty (noErrorHandling) and <> (combineCatchers).

noCatch :: ExceptionCatcher errSource

Don't catch any exception.

catchAnd :: Exception except => (except -> err) -> ExceptionCatcher errSource

Create an ExceptionCatcher from a function.

This will make the catcher aware of exceptions of type except so that when you'll run a catcher against an IO action using raiseIfExc or handledWith, if an exception of this type is thrown, it will be caught and converted to the error type of your web application using the provided function.

combineCatchers :: ExceptionCatcher err -> ExceptionCatcher err -> ExceptionCatcher errSource

Combine two ExceptionCatcher.

The resulting ExceptionCatcher will be able to catch exceptions using the catchers from both arguments.

Running IO actions against an ExceptionCatcher

handledWith :: IO a -> ExceptionCatcher err -> IO (Either err a)Source

Run an IO action by watching for all the exceptions supported by the ExceptionCatcher.

If an exception is thrown somewhere in the action and is caught by the catcher, it will call the function given to catchAnd to convert the exception value to the error type of your scotty application.

If an exception is thrown whose type isn't one for which there's a catcher in the ExceptionCatcher argument, you'll have to catch it yourself or let it be sent to the default handler.

Since runService sets up a defaultHandler, you'll most likely want to directly use raiseIfExc which raises the error value you got from the exception if some exception is thrown or just run some scotty action if everything went smoothly. This means writing a handler that'll set up a response with the proper HTTP status and send some JSON to the client with an informative error message, which... isn't a bad thing :-)