[web-devel] Yesod. Handle Exception.

Michael Snoyman michael at snoyman.com
Tue Jan 25 19:21:39 CET 2011


2011/1/25 Антон Чешков <acheshkov at gmail.com>:
> Hello Michael. Please explain how to handle exceptions in Yesod
> applications.
> For example i would to catch the error generated by unique constraint on
> table.
> runDB $ insert $ Table field1 field2

In general, starting with Persistent 0.4, you will want to use the
monad-peel package[1], which provides facilities for exception
catching in wrapped monads. In Persistent 0.3, you can use the
MonadInvertIO functions available in the neither package[2].
MonadInvertIO and MonadPeelIO are very similar in concept, but
MonadPeelIO is just better[3].

But in your particular case, you don't need to resort to runtime
exceptions: just use insertBy[4] or checkUnique[5].

Michael

[1] http://hackage.haskell.org/package/monad-peel
[2] http://hackage.haskell.org/packages/archive/neither/0.1.0/doc/html/Control-Monad-Invert.html
[3] I wrote MoandInvertIO, Anders Kaseorg wrote MonadPeelIO, and he's
smarter than me.
[4] http://hackage.haskell.org/packages/archive/persistent/0.3.1.3/doc/html/Database-Persist.html#v:insertBy
[5] http://hackage.haskell.org/packages/archive/persistent/0.3.1.3/doc/html/Database-Persist.html#v:checkUnique



More information about the web-devel mailing list