<div dir="ltr">Seems, that you're putting it in the wrong place. Please, publish your code to lpaste [1].<br><br>Hmm.... Maybe you need evaluate `encode req`?</div><br><div class="gmail_quote">On Tue, May 12, 2015 at 9:10 AM Alex <<a href="mailto:alex323@gmail.com">alex323@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Tue, 12 May 2015 05:24:32 +0000<br>
Alexey Shmalko <<a href="mailto:rasen.dubi@gmail.com" target="_blank">rasen.dubi@gmail.com</a>> wrote:<br>
<br>
> Your issue is related to Lazy evaluation. Your function (lookupHeader)<br>
> isn't actually evaluated before you call publishMsg or print. You<br>
> need to put seq or deepseq to overcome this, or put your exception<br>
> handler in place when your function is actually evaluated.<br>
> Unfortunately, you haven't provided the calling code, so I have no<br>
> specific suggestion. Just read some info on Lazy vs. Strict.<br>
><br>
<br>
Per your suggestion, I tried out deepseq, and it doesn't seem to be<br>
working. Below is the code which sends the message to RMQ. As you've<br>
noted, `req' is not fully evaluated at this point:<br>
<br>
publishMsg chan "" "the-queue"<br>
newMsg { msgDeliveryMode = Just Persistent<br>
, msgReplyTo = Just cbQueue<br>
, msgBody = encode req<br>
}<br>
<br>
If I put this line above publishMsg, the problem still exists:<br>
<br>
_ <- return $!! req<br>
<br>
This is counter intuitive, because I would have thought that running<br>
deepseq on req would fully evaluate it to NF, but that doesn't seem to<br>
be the case.<br>
<br>
> Parallel and Concurrent Programming in Haskell Chapter 2 [1] has a<br>
> good description (just skip "The Eval Monad, rpar, and rseq" section<br>
> and continue with deepseq).<br>
> Here is a good chapter on exception [2], if you need one.<br>
><br>
> [1]: <a href="http://chimera.labs.oreilly.com/books/1230000000929/ch02.html" target="_blank">http://chimera.labs.oreilly.com/books/1230000000929/ch02.html</a><br>
> [2]:<br>
> <a href="http://chimera.labs.oreilly.com/books/1230000000929/ch08.html#sec_exceptions" target="_blank">http://chimera.labs.oreilly.com/books/1230000000929/ch08.html#sec_exceptions</a><br>
><br>
> On Tue, May 12, 2015 at 5:11 AM Alex <<a href="mailto:alex323@gmail.com" target="_blank">alex323@gmail.com</a>> wrote:<br>
><br>
> > Hi:<br>
> ><br>
> > I am writing a small application which receives HTTP requests,<br>
> > translates them to JSON, and queues the requests using RabbitMQ.<br>
> ><br>
> > I am using exceptions to handle extreme cases, such as when a<br>
> > client's HTTP request lacks a critical header:<br>
> ><br>
> > lookupHeader :: RequestHeaders -> HeaderName -> Text<br>
> > lookupHeader hs h = decodeUtf8 $ fromMaybe notFound<br>
> > (lookup h hs)<br>
> > where<br>
> > notFound = throw $ MissingHeader $ decodeUtf8 $ original h<br>
> ><br>
> > The problem I am running in to is that the header isn't actually<br>
> > looked up until I call the AMQP library's publishMsg function. If I<br>
> > purposely do not supply a critical header, I get the following<br>
> > error printed to the screen:<br>
> ><br>
> > ConnectionClosedException "ConnectionClosedException<br>
> > \"UNEXPECTED_FRAME<br>
> > - expected content header for class 60, got non content header frame<br>
> > instead\""<br>
> ><br>
> > If I add the following line just above the publishMsg function (to<br>
> > force evaluation), my exception handler is called successfully:<br>
> ><br>
> > print $ encode req<br>
> ><br>
> > As a result, I suspect that this is due to the fact that the "throw<br>
> > MissingHeader" is getting captured by the AMQP library. What's the<br>
> > best way to deal with this situation?<br>
> ><br>
> > --<br>
> > Alex<br>
> > _______________________________________________<br>
> > Beginners mailing list<br>
> > <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> > <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
> ><br>
<br>
<br>
<br>
--<br>
Alex<br>
</blockquote></div>