https://github.com/smuuf/better-php-exceptions
I needed this for my other project, so in the last two days I created a small library for it. I figured it might come handy for anybody else, too (maybe someone also venturing into some PHP “meta” programming). So I’m sharing it!

Currently, it knows only how to transform TypeError thrown by PHP into either ArgumentTypeError or ReturnTypeError. But I built it with additional possibilities in mind.

So I make it, this extension could be useful if I have a global exception handler where I can write something like this
right?

On a second thought, I have a feeling that extra methods are too localized to be usable (and even less – reusable). I cannot picture a dev writing such spacious try-catches where diligently querying each custom method from each custom exception. I would rather prefer a customized error message that will be accessible through __toString as well – so it will allow the uniform handling.

Anyway, could you provide a real life use case, like one you are employing in your project?

My use case is different (I’m using it at specific places where I’m expecting the TypeError to occur). What you’re suggesting might also be a cool use case, but I haven’t tried that one yet.

I wasn’t planning to show this not entirely finished thing just yet (finished meaning version 0.5, which I’m almost finishing up), but since you’re asking… My use case is to replace the hideous after-catch logic at https://github.com/smuuf/primi/blob/780e13f260dd5cdcd3569698dbf952ca19eb641c/src/Structures/FnContainer.php#L144 using BetterException, making it simpler and forward-compatible with PHP8.

source