Connecting Tech Pros Worldwide Help | Site Map

Remove stack trace from Exception message

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#1: Aug 27 '09
My code for formatting a caught Ecxeption into a string is
Expand|Select|Wrap|Line Numbers
  1. $this->message .= '<br>Exception thrown in '.
  2.                 '<br>File: '.basename($msg->getFile(),'.php').
  3.                 '<br>Line: '.$msg->getLine().
  4.                 '<br>Msge: '.$msg->getMessage().
  5.                 '<br>Code: '.$msg->getCode().
  6.                 '<br>------------------------------';
Which is fine, but the stack trace appears in the getMessage() return.
Good for debugging.
But I have remote servers sending emails with this message format containing full addresses of files in the stack trace which is a potential security breach.

How do I prevent getMessage() returning the stack trace.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Aug 27 '09

re: Remove stack trace from Exception message


Quote:

Originally Posted by code green View Post

but the stack trace appears in the getMessage() return.

that is strange, usually the stack trace has its own methods (getTrace(), getTraceAsString()). unless you somehow insert the stack track with the throw (like adding debug_backtrace() or in the exception constructor).
And I’ve never seen that an exception auto-appended the stack trace (well, at least not with my exceptions…)

in the worst case you need to modify the message text via RegEx.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#3: Oct 1 '09

re: Remove stack trace from Exception message


Just to revive this dormant post.
It was only happening when an email failed so was rare.
But it seems to be my fault by doing this
Expand|Select|Wrap|Line Numbers
  1. catch(Exception $e){
  2. throw new Exception($e);
  3. }
which obviously places the whole Exception object into a new Exception object string.
So the stack trace and everything from the original $e is eventually output with the new Exception object with $e->getmessage();
Any advice on re-throwing Exceptions would be appreciated.
I have cleaned the problem with
Expand|Select|Wrap|Line Numbers
  1. catch(Exception $e)
  2. {
  3.     throw new Exception('<br>File: '.basename($e->getFile()).
  4.     '<br>Line: '.$e->getLine().
  5.     '<br>Msge: '.$e->getMessage().
  6.     '<br>Code: '.$e->getCode());
  7. }
Is this how is the Exception information usually passed back along the chain of functions?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Oct 1 '09

re: Remove stack trace from Exception message


if you re-throw a standard Exception, just make sure to pass the correct values.
Expand|Select|Wrap|Line Numbers
  1. catch (Exception $e)
  2. {
  3.     throw new Exception($e->getMessage());
  4. }
otherwise you should define your own Exception classes (which is generally a good idea)

e.g.
Expand|Select|Wrap|Line Numbers
  1. class Exception2 extends Exception
  2. {
  3.   public function __construct($e)
  4.   {
  5.     if ($e instanceof Exception) parent::__construct($e->getMessage());
  6.     elseif (is_string($e)) parent::__construct($e);
  7.     else parent::__construct();
  8.   }
  9. }
another possibility would be setting the __toString() method of the Exception (though I didn’t try that for this scenario)
Reply