> Hello PHP Internals,
>
> I would like to propose a discussion regarding two current limitations in
> PHP's exception handling system that I believe could be addressed to
> improve flexibility and developer experience.
>
> A few years ago I found that a library printed error traces wrong.
> After a little research I found that there was a mix of 3rd party
> integration error + raised error around the current bridge implementation
> (http client).
>
> There were several PHP applications with microservices architecture which
> I had access to (docker + sources).
>
> So having the message and traces I'd like to have an error chain as it can
> be done chaining several errors through a new Exception(previous: $e).
> But PHP does not allow you to manually implement Throwable. Instead you
> should extend Exception. But after that you still cannot override the
> getTrace() method because it's final.
>
> So my proposal is pretty simple: Remove both restrictions.
>
> 1. Allow user classes to implement Throwable interface directly
>
> User classes cannot implement the Throwable interface now.
>
> ```php
> class MyCustomThrowable implements Throwable {}
>
> // Fatal error: Class MyCustomThrowable cannot implement interface
> Throwable, extend Exception or Error instead
> ```
>
>
> 2. Make getTrace() non-final or provide alternative customization mechanism
>

The trace is stored in a private property, so you can use reflection to
change it.

new \ReflectionProperty(\Exception::class, 'trace')->setValue($e, $trace)

Nicolas

Reply via email to