<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://prendreuncafe.com/blog/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Prendre un Café - Tag - exceptions</title>
  <link>http://prendreuncafe.com/blog/</link>
  <atom:link href="http://prendreuncafe.com/blog/feed/tag/exceptions/rss2" rel="self" type="application/rss+xml"/>
  <description></description>
  <language>fr</language>
  <pubDate>Tue, 30 Dec 2008 16:33:00 +0100</pubDate>
  <copyright>Contenus sous licence Creative Commons BY-SA</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Symfony, une redirection 302 et une exception sont dans un bateau</title>
    <link>http://prendreuncafe.com/blog/post/2008/01/12/Symfony-une-redirection-302-et-une-exception-sont-dans-un-bateau</link>
    <guid isPermaLink="false">urn:md5:5351e7d789b0a5e75088fbee0c7270cd</guid>
    <pubDate>Sat, 12 Jan 2008 20:56:00 +0100</pubDate>
    <dc:creator>NiKo</dc:creator>
        <category>Dev</category>
        <category>bestpractices</category><category>exceptions</category><category>php</category><category>symfony</category>    
    <description>    &lt;p&gt;Ah la la. C'est assez rare pour être souligné, mais je viens de me battre avec &lt;a href=&quot;http://www.symfony-project.com&quot; hreflang=&quot;en&quot;&gt;Symfony&lt;/a&gt; pendant près de deux heures avec un problème assez déroutant de prime abord : lorsque dans une action vous faites quelque chose d'aussi anodin que ceci :&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;public &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; executeBar&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    try
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Some stuff here, if successful redirect user to somewhere else&lt;/span&gt;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setFlash&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'notice'&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'Action was successful'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;redirect&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'@whatever_route'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
    catch &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Exception &lt;span style=&quot;color: #0000ff;&quot;&gt;$e&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
       &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getRequest&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setError&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'errors'&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'Something has failed somewhere, sorry dude'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
       &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;logMessage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Big boo boo occured: '&lt;/span&gt;.&lt;span style=&quot;color: #0000ff;&quot;&gt;$e&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getMessage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'err'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
       &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; sfView::&lt;span style=&quot;color: #006600;&quot;&gt;SUCCESS&lt;/span&gt;;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Et bien dans ce cas là, la redirection s'opère MAIS l'ensemble du rendu sera tout de même produit et envoyé au navigateur &lt;sup&gt;[&lt;a href=&quot;http://prendreuncafe.com/blog/post/2008/01/12/#pnote-937-1&quot; id=&quot;rev-pnote-937-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; - ce qui peut s'avérer très couteux sur un site internet au final &lt;sup&gt;[&lt;a href=&quot;http://prendreuncafe.com/blog/post/2008/01/12/#pnote-937-2&quot; id=&quot;rev-pnote-937-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;. La raison en est très simple, la méthode &lt;code&gt;redirect()&lt;/code&gt; de la classe &lt;code&gt;sfActions&lt;/code&gt; met fin à l'execution par ce moyen &lt;a href=&quot;http://www.symfony-project.org/api/1_0/sfAction#method_redirect&quot; hreflang=&quot;en&quot;&gt;que l'on peut qualifier de hardi&lt;/a&gt; :&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;public &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; redirect&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$url&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$statusCode&lt;/span&gt; = &lt;span style=&quot;color: #cc66cc;&quot;&gt;302&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;$url&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getController&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;genUrl&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$url&lt;/span&gt;, &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
    &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sfConfig::&lt;span style=&quot;color: #006600;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'sf_logging_enabled'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getContext&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getLogger&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'{sfAction} redirect to &amp;quot;'&lt;/span&gt;.&lt;span style=&quot;color: #0000ff;&quot;&gt;$url&lt;/span&gt;.&lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;quot;'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getController&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;redirect&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$url&lt;/span&gt;, &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$statusCode&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
    throw &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; sfStopException&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Oui, vous avez bien lu, on interrompt le script en levant une exception, ici de type &lt;code&gt;&lt;a href=&quot;http://www.symfony-project.org/api/1_0/sfStopException&quot; hreflang=&quot;en&quot;&gt;sfStopException&lt;/a&gt;&lt;/code&gt;. Le problème, c'est que dans mon exemple précédent, la méthode redirect() est appellée dans un bloc &lt;code&gt;try { } catch { }&lt;/code&gt;, et donc l'exception levée est interceptée et l'action n'est au final pas stoppée. Vicieux, hein ?&lt;/p&gt;


&lt;p&gt;Pour régler le problème, on peut par exemple toujours effectuer ses appels à la méthode &lt;code&gt;redirect()&lt;/code&gt; en dehors d'un block &lt;code&gt;try { } catch { }&lt;/code&gt; &lt;sup&gt;[&lt;a href=&quot;http://prendreuncafe.com/blog/post/2008/01/12/#pnote-937-3&quot; id=&quot;rev-pnote-937-3&quot;&gt;3&lt;/a&gt;]&lt;/sup&gt;, ou encore tester le type de l'exception levée. Dans notre exemple, cette dernière solution ressemblerait à ça :&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;public &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; executeFoo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    try
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Some stuff here, if successful redirect user to somewhere else&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/// ...&lt;/span&gt;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setFlash&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'notice'&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'Action was successful'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;redirect&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'@whatever_route'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
    catch &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sfStopException &lt;span style=&quot;color: #0000ff;&quot;&gt;$e&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; sfView::&lt;span style=&quot;color: #006600;&quot;&gt;HEADER_ONLY&lt;/span&gt;;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
    catch &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Exception &lt;span style=&quot;color: #0000ff;&quot;&gt;$e&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getRequest&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;setError&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'errors'&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'Something has failed somewhere, sorry dude'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
      &lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;logMessage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Big boo boo occured: '&lt;/span&gt;.&lt;span style=&quot;color: #0000ff;&quot;&gt;$e&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getMessage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'err'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; sfView::&lt;span style=&quot;color: #006600;&quot;&gt;SUCCESS&lt;/span&gt;;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;C'est pas super sexy, mais ça fonctionne, et ça a le mérite de m'inspirer deux morales à cette histoire :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;tester le type des exceptions que l'on catche, c'est bien&lt;/li&gt;
&lt;li&gt;lever une exception pour arrêter un script, c'est super cracra et mériterait éventuellement d'être patché &lt;img src=&quot;/blog/themes/battlestar/smilies/wink.gif&quot; alt=&quot;;)&quot; class=&quot;smiley&quot; /&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Edit: &lt;/strong&gt; Je m'exprime mal, c'est pas &lt;em&gt;super cracra&lt;/em&gt;, c'est juste que ça introduit une petite complexité supplémentaire. Mais l'utilité de la chose est bien entendu totalement avérée si on a besoin d'effectuer des opérations particulières avant la fin du script ( ce qui est rarement le cas, enfin chez moi).&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://prendreuncafe.com/blog/post/2008/01/12/#rev-pnote-937-1&quot; id=&quot;pnote-937-1&quot;&gt;1&lt;/a&gt;] Avec tout ce que ça comporte de requête SQL et de templates calculés &lt;em&gt;pour rien&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://prendreuncafe.com/blog/post/2008/01/12/#rev-pnote-937-2&quot; id=&quot;pnote-937-2&quot;&gt;2&lt;/a&gt;] Et oui bien sûr, faire un &lt;code&gt;redirect()&lt;/code&gt; dans un bloc &lt;code&gt;try catch&lt;/code&gt; c'est pas forcément le meilleur endroit, mais c'est teeeellement pratique &lt;img src=&quot;/blog/themes/battlestar/smilies/tongue.gif&quot; alt=&quot;:p&quot; class=&quot;smiley&quot; /&gt;&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://prendreuncafe.com/blog/post/2008/01/12/#rev-pnote-937-3&quot; id=&quot;pnote-937-3&quot;&gt;3&lt;/a&gt;] Qui sera donc exécuté de toute façon si aucune exception n'est levée.&lt;/p&gt;&lt;/div&gt;&lt;hr/&gt;&lt;p style='margin:.5em 0;padding:.5em;border:1px solid #333;background:#eee;color:#222'&gt;&lt;small&gt;Ce billet intitulé &lt;a href='http://prendreuncafe.com/blog/post/2008/01/12/Symfony-une-redirection-302-et-une-exception-sont-dans-un-bateau'&gt;Symfony, une redirection 302 et une exception sont dans un bateau&lt;/a&gt; a été rédigé par &lt;a href='http://prendreuncafe.com/cv'&gt;Nicolas Perriault&lt;/a&gt; et publié sur le blog &lt;a href='http://prendreuncafe.com/blog/'&gt;Prendre un Café&lt;/a&gt; sous licence &lt;a href='http://creativecommons.org/licenses/by-nc-sa/2.5/'&gt;Creative Commons BY-NC-SA&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;</description>
    
    
    
      </item>
    
</channel>
</rss>
