[web-devel] A few questions about Yesod

Iustin Pop iustin at google.com
Thu Dec 30 14:31:57 CET 2010

On Thu, Dec 30, 2010 at 03:09:16PM +0200, Michael Snoyman wrote:
> Very good questions, answers below.
> On Thu, Dec 30, 2010 at 1:01 PM, Iustin Pop <iustin at google.com> wrote:
> > Hi all,
> >
> > I just started looking at Yesod and its associated libs (hamlet, etc.)
> > and it is very interesting, thanks.
> >
> > However, I'm confused by a few things and the docs are not helping, so
> > please bear my beginner questions.
> >
> > First: hamlet uses '.' as function application, instead of the usual
> > space. How can I then use a qualified name (e.g. Data.List.nub)? If I
> > use it normally, it errors out on me. Must be something very trivial but
> > I cannot find a way.
> Hamlet uses both '.' and space as function application, and therefore
> qualified names are not supported. I work around this usually by
> creating an alias for the function I want locally. I know this can be
> inelegant; if you have any ideas, I'm all ears.

Yep, that's what I'm using too now, but it becomes cumbersome quickly,
especially when using records…

I'm not sure what was the original impetus to use . (too) as function
application if space is accepted too. I'm thinking reverting that
decision would make the code look more like regular Haskell.

A simpler alternative (not sure if easily doable) would be to allow
escaping of the dot, e.g. $Data\.List\.nub.mylist$; it's ugly, but…

> > Second: Yesod.Json says “Due to the limited nature of the JSON format,
> > you can create any valid JSON document you wish using only jsonScalar,
> > jsonList and jsonMap”. This seems false to me, as jsonScalar only deals
> > with JSON strins, and not JSON numbers or the constants (true, false,
> > null). In the meantime I'm using a hack for numbers using jsonRaw, but I
> > think some more functionality needs to be added to it for non-trivial
> > usage.
> Quite correct. I'm working on Yesod 0.7 right now, which will be
> offloading the JSON work to John Millikin's json-types package which
> properly supports numbers, booleans and nulls.

Cool, that sounds good.

> > And last: I don't understand how runDB and the persistent framework is
> > supposed to do error handling. Case in point: let's say one tries to
> > insert a duplicate unique key in a table, which fails as expected "user
> > error (SQLite3 returned ErrorConstraint while attempting to perform
> > step.)". The problem is any other use of that particular REST resource,
> > even with different parameters, results in "user error (SQLite3 returned
> > ErrorMisuse while attempting to perform bind text.)", and I don't know
> > if this is due to my usage of the framework or due to something else.
> > The interesting part is that other resources are working fine.
> That sounds like a bug in the SQLite backend to be honest. Could you
> send me a test case that breaks it? It is much appreciated.

I've just generated a sample site, and added this new resource:
  /test/#String TestR GET

with the associated handler:

getTestR :: String -> Handler RepHtml 
getTestR name = do 
  runDB $ Test.insert (User name Nothing) 
  defaultLayout [$hamlet| Ok |] 

How it behaves:

/test/a → OK (as expected)
/test/a → SQLite3 returned ErrorConstraint while attempting to perform
step (expected)
/test/a → SQLite3 returned ErrorMisuse while attempting to perform bind
text (unexpected)
/test/b → the same message as above, totally unexpected

This persists until application restart.

Again, this might be just due to my wrong usage of the library, but I
haven't found any docs on how DB errors should be handled.

In use persistent-sqlite-0.3.0, persistent-, yesod-0.6.7.

thanks a lot for the fast answers!

More information about the web-devel mailing list