Pruning Stacktrace for Custom Exceptions in PHP

debuggingexceptionsPHPstacktrace

I am working on a project in PHP that does a lot of input validation and can throw different custom exception classes in various layers of the application. To make the project code easier to read, I've displaced all the validation code into static methods of a validation class, which performs the check and throws the appropriate exception if necessary. My problem is, the exception stacktrace displays the entire call stack down into the validation class, but I feel like the extra information is not helpful and will only serve to confuse someone trying to debug the location of the real problem. Should I simply rely on informative exception messages or is it possible to trim the trace log in some way.

Best Answer

Even if you could, I don't think you should mess with the stack trace. It messes with the programmer's mental model of how exceptions work. When I look at the top line of an exception trace, I expect to see a throw.

Any time you do something that's not "expected", it takes me (ie someone using your library) time to figure out what's actually going on. By keeping surprises to an absolute minimum, you make using your library easier and more intuitive.

Plus, I'd be really, really surprised if you were using set_exception_handler from an input validation library!