# R28969 - in /trunk: egroupware/xajax.php phpgwapi/inc/common\_functions.inc.php

**URL:** <http://discourse.egroupware.org/t/r28969-in-trunk-egroupware-xajax-php-phpgwapi-inc-common-functions-inc-php/36482>\
**Category:** eGW-svn\
**Created:** [January 14, 2010, 11:50pm UTC](http://discourse.egroupware.org/t/r28969-in-trunk-egroupware-xajax-php-phpgwapi-inc-common-functions-inc-php/36482 "2010-01-14T23:50:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralf\_Becker\_6](http://discourse.egroupware.org/user_avatar/discourse.egroupware.org/ralf_becker_6/32/1306_2.png) [@Ralf\_Becker\_6](http://discourse.egroupware.org/u/Ralf_Becker_6)\
**Post date:** [January 14, 2010, 11:50pm UTC](http://discourse.egroupware.org/t/r28969-in-trunk-egroupware-xajax-php-phpgwapi-inc-common-functions-inc-php/36482/1 "2010-01-14T23:50:06Z")

</div>

Author: ralfbecker  
New Revision: 28969

URL: [http://www.egroupware.org/viewvc/egroupware?rev=28969&view=rev](http://www.egroupware.org/viewvc/egroupware?rev=28969&view=rev)  
Log:  
refractored our exception handler a bit, to allow using part of it from ajax too. Ajax logs now the exception to the error\_log and only shows a trace to the user if enabled in the config, like the rest of EGroupware

Modified:  
trunk/egroupware/xajax.php  
trunk/phpgwapi/inc/common\_functions.inc.php

# Modified: trunk/egroupware/xajax.php URL: [http://www.egroupware.org/viewvc/egroupware/trunk/egroupware/xajax.php?rev=28969&r1=28968&r2=28969&view=diff](http://www.egroupware.org/viewvc/egroupware/trunk/egroupware/xajax.php?rev=28969&r1=28968&r2=28969&view=diff)

— trunk/egroupware/xajax.php (original)  
+++ trunk/egroupware/xajax.php Fri Jan 15 00:50:06 2010  
@@ -33,7 +33,7 @@  
}

/\*\*

- 
  - Exception handler for xajax, return the message (and trace) as alert() to the user

- 
  - Exception handler for xajax, return the message (and trace, if enabled) as alert() to the user
  - 
  - Does NOT return!
  - 

@@ -41,8 +41,20 @@  
\*/  
function ajax\_exception\_handler(Exception $e)  
{

- // logging all exceptions to the error\_log
- if (function\_exists(’\_egw\_log\_exception’))
- {
- 

```
  _egw_log_exception($e,$message);

```

- }  
$response = new xajaxResponse();

- $response-\>addAlert($e-\>getMessage()."\n\n".$e-\>getTraceAsString());

- $message .= ($message ? “\n\n” : ‘’).$e-\>getMessage();

- 
- // only show trace (incl. function arguments) if explicitly enabled, eg. on a development system

- if ($GLOBALS[‘egw\_info’][‘server’][‘exception\_show\_trace’])

- {

- 

```
  $message .= "\n\n".$e->getTraceAsString();

```

- }

- $response-\>addAlert($message);  
$response-\>printOutput();

# Modified: trunk/phpgwapi/inc/common\_functions.inc.php URL: [http://www.egroupware.org/viewvc/egroupware/trunk/phpgwapi/inc/common\_functions.inc.php?rev=28969&r1=28968&r2=28969&view=diff](http://www.egroupware.org/viewvc/egroupware/trunk/phpgwapi/inc/common_functions.inc.php?rev=28969&r1=28968&r2=28969&view=diff)

— trunk/phpgwapi/inc/common\_functions.inc.php (original)  
+++ trunk/phpgwapi/inc/common\_functions.inc.php Fri Jan 15 00:50:06 2010  
@@ -1422,6 +1422,41 @@  
}

/\*\*

- 
  - Clasify exception for a headline and log it to error\_log, if not running as cli

- 
  - 

- 
  - @param Exception $e

- 
  - @param string &$headline

- \*/  
+function \_egw\_log\_exception(Exception $e,&$headline=null)  
+{
- if ($e instanceof egw\_exception\_no\_permission)
- {
- 

```
  $headline = try_lang('Permission denied!');

```

- }
- elseif ($e instanceof egw\_exception\_db)
- {
- 

```
  $headline = try_lang('Database error');

```

- }
- elseif ($e instanceof egw\_exception\_wrong\_userinput)
- {
- 

```
  $headline = '';	// message contains the whole message, it's usually no real error but some input validation

```

- }
- else
- {
- 

```
  $headline = try_lang('An error happened');

```

- }
- // log exception to error log, if not running as cli,
- // which outputs the error\_log to stderr and therefore output it twice to the user
- if(!isset($\_SERVER[‘HTTP\_HOST’]) || $GLOBALS[‘egw\_info’][‘flags’][‘no\_exception\_handler’] == ‘cli’)
- {
- 

```
  error_log($headline.': '.$e->getMessage());

```

- 

```
  foreach(explode("\n",$e->getTraceAsString()) as $line) error_log($line);

```

- 

```
  error_log('# Instance='.$GLOBALS['egw_info']['user']['domain'].', User='.$GLOBALS['egw_info']['user']['account_lid'].', URL='.

```

- 

```
  	($_SERVER['HTTPS']?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

```

- }  
+}
- 

+/\*\*

- Fail a little bit more gracefully then an uncought exception
- 
- Does NOT return  
@@ -1430,22 +1465,9 @@  
\*/  
function egw\_exception\_handler(Exception $e)  
{

- if ($e instanceof egw\_exception\_no\_permission)
- {
- 

```
  $headline = try_lang('Permission denied!');

```

- }
- elseif ($e instanceof egw\_exception\_db)
- {
- 

```
  $headline = try_lang('Database error');

```

- }
- elseif ($e instanceof egw\_exception\_wrong\_userinput)
- {
- 

```
  $headline = '';	// message contains the whole message, it's usually no real error but some input validation

```

- }
- else
- {
- 

```
  $headline = try_lang('An error happened');

```

- }

- // logging all exceptions to the error\_log (if not cli) and get headline
- \_egw\_log\_exception($e,$headline);
- // exception handler for cli (command line interface) clients, no html, no logging  
if(!isset($\_SERVER[‘HTTP\_HOST’]) || $GLOBALS[‘egw\_info’][‘flags’][‘no\_exception\_handler’] == ‘cli’)  
{  
@@ -1456,12 +1478,6 @@  
}  
exit($e-\>getCode() ? $e-\>getCode() : 9999); // allways give a non-zero exit code  
}

- // logging all exceptions to the error\_log
- error\_log($headline.’: '.$e-\>getMessage());
- foreach(explode("\n",$e-\>getTraceAsString()) as $line) error\_log($line);
- error\_log(’# Instance=’.$GLOBALS[‘egw\_info’][‘user’][‘domain’].’, User=’.$GLOBALS[‘egw\_info’][‘user’][‘account\_lid’].’, URL=’.
- 

```
  ($_SERVER['HTTPS']?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

```

- // regular GUI exception  
if (!isset($GLOBALS[‘egw\_info’][‘flags’][‘no\_exception\_handler’]))  
{  
@@ -1494,9 +1510,9 @@  
header(‘HTTP/1.1 401 Unauthorized’);  
header(‘X-WebDAV-Status: 401 Unauthorized’, true);  
}
- if (is\_object($GLOBALS[‘egw’]) && isset($GLOBALS[‘egw’]-\>common))
- {
- 

```
  $GLOBALS['egw']->common->egw_exit();

```

- if (is\_object($GLOBALS[‘egw’]))
- {
- 

```
  common::egw_exit();

```

}  
exit;  
}

* * *

Throughout its 18-year history, RSA Conference consistently attracts the  
world’s best and brightest in the field, creating opportunities for Conference  
attendees to learn about information security’s most important issues through  
interactions with peers, luminaries and emerging and established companies.  
[http://p.sf.net/sfu/rsaconf-dev2dev](http://p.sf.net/sfu/rsaconf-dev2dev)

* * *

eGroupWare-cvs mailing list  
[eGroupWare-cvs@lists.sourceforge.net](mailto:eGroupWare-cvs@lists.sourceforge.net)  
[https://lists.sourceforge.net/lists/listinfo/egroupware-cvs](https://lists.sourceforge.net/lists/listinfo/egroupware-cvs)
