Hi,

I created patches to Context, ConsoleController, PageWebController and FrontWebController that allows you to catch AgaviExceptions and forward them to a user-defined module/action pair. This basically sprung from the fact that our users will get a heart attack if they click something and suddenly a big red box with bold red text and some arcane code in it appears on the screen :)

This is a very unobtrusive patch, if you haven't defined AG_ERROR_MODULE and AG_ERROR_ACTION (in settings.ini) to point to a module/action pair that handles the exception (ie. makes a pretty error message), the exception is printed like it always has been, ie stack trace is printed to the screen. Same thing happens if an exception is thrown in the exception handler module (or in the path from handleException()).


Cheers,
Markus

--
Markus Lervik, CTO      | "In God we Trust - all others
Necora Systems Ltd      |  must submit an X.509 certificate"
http://www.necora.fi    |    - Charles Forsythe
[EMAIL PROTECTED] | (X.509 certificate available at
+358-40-832 6709        | http://www.necora.fi/markus/x509.crt)
Index: D:/Projects/agavi/controller/ConsoleController.class.php
===================================================================
--- D:/Projects/agavi/controller/ConsoleController.class.php    (revision 447)
+++ D:/Projects/agavi/controller/ConsoleController.class.php    (working copy)
@@ -84,19 +84,12 @@
                        // make the first request
                        $this->forward($moduleName, $actionName);
 
-               } catch (AgaviException $e) {
-
-                       $e->printStackTrace('plain');
-
                } catch (Exception $e) {
+            
+            $this->context->handleException($e);
+            
+        }
 
-                       // most likely an exception from a third-party library
-                       $e = new AgaviException($e->getMessage());
-
-                       $e->printStackTrace('plain');
-
-               }
-
        }
 
 }
Index: D:/Projects/agavi/controller/FrontWebController.class.php
===================================================================
--- D:/Projects/agavi/controller/FrontWebController.class.php   (revision 447)
+++ D:/Projects/agavi/controller/FrontWebController.class.php   (working copy)
@@ -84,21 +84,12 @@
                        // make the first request
                        $this->forward($moduleName, $actionName);
 
-               } catch (AgaviException $e)
-               {
+               } catch (Exception $e) {
+         
+            $this->context->handleException($e);
+               
+        }
 
-                       $e->printStackTrace();
-
-               } catch (Exception $e)
-               {
-
-                       // most likely an exception from a third-party library
-                       $e = new AgaviException($e->getMessage());
-
-                       $e->printStackTrace();
-
-               }
-
        }
 
 }
Index: D:/Projects/agavi/controller/PageWebController.class.php
===================================================================
--- D:/Projects/agavi/controller/PageWebController.class.php    (revision 447)
+++ D:/Projects/agavi/controller/PageWebController.class.php    (working copy)
@@ -56,17 +56,12 @@
                        // make the first request
                        $this->forward($moduleName, $actionName);
 
-               } catch (AgaviException $e) {
-                       $e->printStackTrace();
-
                } catch (Exception $e) {
+            
+            $this->context->handleException($e);
+            
+        }
 
-                       // most likely an exception from a third-party library
-                       $e = new AgaviException($e->getMessage());
-                       $e->printStackTrace();
-
-               }
-
        }
 
 }
Index: D:/Projects/agavi/core/Context.class.php
===================================================================
--- D:/Projects/agavi/core/Context.class.php    (revision 447)
+++ D:/Projects/agavi/core/Context.class.php    (working copy)
@@ -44,7 +44,8 @@
                $user            = null;
        protected static
                $instances                      = null,
-               $profiles                               = array();
+               $profiles                               = array(),
+        $exceptionHandled = false;
 
        // 
+-----------------------------------------------------------------------+
        // | METHODS                                                            
   |
@@ -423,7 +424,58 @@
                return $this->user;
 
        }
+    
+    // 
-------------------------------------------------------------------------
+    
 
+    /**
+     * Forward exceptions to a specified module/action pair
+     * if they are defined
+     * 
+     * @param $e Exception to be forwarded
+     * @author Markus Lervik ([EMAIL PROTECTED])
+     * @since 0.10.2
+     */
+    public function handleException(Exception $e)
+    {
+        
+       if (defined('AG_ERROR_MODULE') && defined('AG_ERROR_ACTION') &&
+            $this->controller->moduleExists(AG_ERROR_MODULE) && 
+            $this->controller->actionExists(AG_ERROR_MODULE, AG_ERROR_ACTION) 
&&
+            self::$exceptionHandled === false)  {
+              
+            try {
+                
+                self::$exceptionHandled = true;
+                $this->request->setParameter('exception', $e);
+                $this->controller->forward(AG_ERROR_MODULE, AG_ERROR_ACTION);
+                
+            } catch (Exception $e) {
+                
+                $e = new AgaviException('Exception handler failed: ' . 
$e->getMessage());
+                $this->handleException($e);
+                
+            }
+                        
+            
+        } else { 
+        
+                            
+            if ($e instanceof AgaviException) {
+            
+                $e->printStackTrace();
+            
+            } else {
+            
+                $e = new AgaviException($e->getMessage());
+                $e->printStackTrace();
+                
+            }
+                         
+        }          
+
+    }
+        
 }
 
 ?>
_______________________________________________
agavi-dev mailing list
[email protected]
http://labworkz.com/cgi-bin/mailman/listinfo/agavi-dev

Reply via email to