473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove stack trace from Exception message

code green
1,726 Recognized Expert Top Contributor
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 12498
Dormilich
8,658 Recognized Expert Moderator Expert
@code green
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.
Aug 27 '09 #2
code green
1,726 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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
2518
by: Andy Fish | last post by:
Hi, in my c# code I have something like this: try { ... } catch (Exception ex) { ... throw ex; }
3
5463
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 (Exception ex) { dumpEx(ex);
13
25568
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 possible to determine the amount of memory used in the current stack? Any assistance would be appreciated. Ben
0
3678
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 code that does the call looks roughly like: try { // do reflective call response = method.Invoke(obj, params); }
2
4393
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
3721
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
4200
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 in one fell swoop. You can reproduce this using the code listed under . .NET 2.0 is used. The only description of the exception found at Microsoft was not helpful. As the exception occurs in
3
7229
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 empty. How do I append the Stack Trace in the custom exception? I can get the inner exception stack trace from the system exceptions, but how do I get my own stack trace information. Thanks in advance
3
7732
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 std::bad_alloc exception if memory could not be allocated. I have never handled the bad_alloc, so, what I expect here is a stack trace from windows showing the excact location where the exception was thrown. But, to my surprise it gives only the windows ...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.