Remove stack trace from Exception message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    Remove stack trace from Exception message

    My code for formatting a caught Ecxeption into a string is
    Code:
    $this->message .= '<br>Exception thrown in '.
    				'<br>File: '.basename($msg->getFile(),'.php').
    				'<br>Line: '.$msg->getLine().
    				'<br>Msge: '.$msg->getMessage().
    				'<br>Code: '.$msg->getCode().
    				'<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
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by code green
    but the stack trace appears in the getMessage() return.
    that is strange, usually the stack trace has its own methods (getTrace(), getTraceAsStrin g()). 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.

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      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
      Code:
      catch(Exception $e){
      throw new Exception($e);
      }
      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
      Code:
      catch(Exception $e)
      {
      	throw new Exception('<br>File: '.basename($e->getFile()).
      	'<br>Line: '.$e->getLine().
      	'<br>Msge: '.$e->getMessage().
      	'<br>Code: '.$e->getCode());
      }
      Is this how is the Exception information usually passed back along the chain of functions?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        if you re-throw a standard Exception, just make sure to pass the correct values.
        Code:
        catch (Exception $e)
        {
            throw new Exception($e->getMessage());
        }
        otherwise you should define your own Exception classes (which is generally a good idea)

        e.g.
        Code:
        class Exception2 extends Exception
        {
          public function __construct($e)
          {
            if ($e instanceof Exception) parent::__construct($e->getMessage());
            elseif (is_string($e)) parent::__construct($e);
            else parent::__construct();
          }
        }
        another possibility would be setting the __toString() method of the Exception (though I didn’t try that for this scenario)

        Comment

        Working...