<div dir="ltr">Your issue is related to Lazy evaluation. Your function (lookupHeader) isn't actually evaluated before you call publishMsg or print. You need to put seq or deepseq to overcome this, or put your exception handler in place when your function is actually evaluated. Unfortunately, you haven't provided the calling code, so I have no specific suggestion. Just read some info on Lazy vs. Strict.<div><br></div><div>Parallel and Concurrent Programming in Haskell Chapter 2 [1] has a good description (just skip "The Eval Monad, rpar, and rseq" section and continue with deepseq).</div><div>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">http://chimera.labs.oreilly.com/books/1230000000929/ch02.html</a><br></div><div>[2]: <a href="http://chimera.labs.oreilly.com/books/1230000000929/ch08.html#sec_exceptions">http://chimera.labs.oreilly.com/books/1230000000929/ch08.html#sec_exceptions</a></div></div><br><div class="gmail_quote">On Tue, May 12, 2015 at 5:11 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">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 client's<br>
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 looked<br>
up until I call the AMQP library's publishMsg function. If I purposely<br>
do not supply a critical header, I get the following error printed to<br>
the screen:<br>
<br>
ConnectionClosedException "ConnectionClosedException \"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 best<br>
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>
</blockquote></div>