473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Remove stack trace from Exception message

code green
1,726 Expert 1GB
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.
Aug 27 '09 #1
3 12310
Dormilich
8,658 Expert Mod 8TB
@code green
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.
Aug 27 '09 #2
code green
1,726 Expert 1GB
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?
Oct 1 '09 #3
Dormilich
8,658 Expert Mod 8TB
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)
Oct 1 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Andy Fish | last post by:
Hi, in my c# code I have something like this: try { ... } catch (Exception ex) { ... throw ex; }
3
by: Mike Schilling | last post by:
Instances of SystemOutOfMemoryException do not contain a stack trace. Easy test to verify this: class OOM { public static void Main() { try { Object arr = new Object; } catch...
13
by: Ben R. Bolton | last post by:
The documentation indicates that the threads "default stack size" is 1MB. The work "default" implies that it can be changed. Is it possible to change the StackSize in .NET? If so how? Is it...
0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
2
by: Lasse Vågsæther Karlsen | last post by:
If I got the following code: try { // something that might throw an exception } catch (Exception ex) { // Log contents of ex here throw;
3
by: barker7 | last post by:
I am calling an unmanaged C++ method from C#. When an exception occurs in the C++ code, it gets caught in our C# code. CSharpMethod() { try { NativeCPPMethod(); } catch (Exception e)
2
by: Michael Meckelein | last post by:
I get "Value of '3720' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum' exception if I remove rows in a dataGridView while scrolling from the top to the end of the grid...
3
by: Dave Anson | last post by:
I have a custom exception and I want to write the information to the event log, including the Stack Trace. I can create the message and write to the event log no problem, but the Stack Trace is...
3
by: Sendil kumar | last post by:
Hi All, Problem Stetement:I have a problem in getting stack trace when I ues std::exception. In my code, I allocate virtual memory for certain kind of processing and will throw the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.