PHP Laravel extending the error handler class - php

I'm trying to build a specific error "stack" for my applications. I have a small module system in place, and I want to be able to add the error handlers from the module to the current error stack. However, there are some error handlers that I want to be checked before the module error handlers, but those need registered before the module error handlers, in case there is in error in the module's bootstrapping process.
The stack I'm going for looks like:
Logging Error handlers
Alert Error handlers (firing off emails to developers if necessary)
Module error handlers
Application error handlers
Default fallbacks
When the application starts, the logging, alert, and default fallbacks are placed on the stack immediately. I want to be able to stub the application error handlers in next, and then the module error handlers once the module is determined and loaded. As far as I can see, error handlers can only be placed on the top or bottom of the stack; not inserted in the middle.
Is there a way to insert into the error handler? If not, is there a way to register a different error handling class (one extended from the built-in one)?

In the interest of time, I managed to come up with a solution that works. It's not what I was really looking for, but I'll share it in case anyone else needs to do something like this.
My approach was to extend the Illuminate\Foundation\Application and override the registerExceptionProvider() method to register my own ExceptionServiceProvider. This provider extends from Illuminate\Exception\ExceptionServiceProvider and overrides the registerHandler() method. In there, I register my Handler in the same manner the original provider registers the default handler.
I added insert* versions of each of the error methods provided by the default Application and an insertError method to the Handler that does the actual insert into the error handler stack. Lastly, I altered the start.php file to create my Application instead of the default one.
As I mentioned, I don't really like this approach; I feel it's more heavy lifting that what should have to be done. It seems like Laravel should have a better way to extend the actual error handler, or at least an interface to order the error handlers being registered more precisely.

Related

phpUnit test code that sets up error handler

I have some php code in a class that uses inheritance and requires and during that loading process, the error handler is setup. This interferes with my phpUnit test as it captures the exceptions/errors that phpUnit needs to run my tests. This is a large project and I can't just go change stuff. I can add stuff, though.
I cannot change the existing error handler code and as such I am looking for a way to make phpUnit be able to prevent the error handlers being called. I thought about overriding the call to set_error_handler, but this as expected gives the "cannot redeclare" error.
Currently I manually edit setup portion of the error handler by commenting the set_error_handler function call, but it would be nice if I could make this automatic without changing the source code (only changing the test code).
Is my problem clearly enough described?
Is what I need possible without adding runtime modules or changing the source code, i.e. just change the unittest php file?
Using php 5.5.

Unexpected error annotations everywhere since I installed version Oxygen

I installed the last version (Oxygen) of Eclipse for PHP. But now there are lots of error annotations that I think they shouldn't be.
Almost all of them have to do with Exception:
throw new Exception('Exception message');
The annotation message in the popup hint is like so:
Exception cannot be resolved to a type
And the hint offers me some quick fixes, which are using the Exception class declared in libraries imported with Composer.
Why is that? As far as I know, Exception is still an internal PHP class (no need to import it). I know I should be using more specific Exception classes, but for now, Exception works for me. And it shouldn't be marked as an error in Eclipse. Prior versions didn't detect this as an error. Furthermore the application runs without any problem.
Is this a bug? Otherwise, how do I disable this type of error annotation?
EDIT:
Another annoying issue happening is that the code assistant is not displaying any php internal function. Only functions, classes and methods declared in my app or in imported libraries. For instance, if I type:
str
the code assistant displays classes from Doctrine, Geocoder, etc, and imported functions like "strip_quotes", but nothing about strstr, str_pad, strpos, etc
Did you forgot about namespaces and PSR-4?
throw new \Exception('Exception message');
So when you are using a class which must be autoloaded, you must declare it via use or call it with full path (with namespace).
If you don`t want to write correct code and this message is annoying you, then I'm pretty sure that you can turn off this message via Eclipse configuration.

Laravel 5 - Add exceptions rendering logic from a package

I'm developing a package for Laravel 5 and I'd like to add the rendering logic of my own exceptions to the default ExceptionHandler.
I don't want to replace the default Laravel 5 exception handler, just want to let the application where my package is installed be aware of how my package exceptions should be rendered.
How can I do that? Thank you :)
If you wanted to replace the application exception handler, you could do so in your service provider. While it might be nice to extend from the application exception handler it would be difficult because it might not be named App\Excpetions\Handler if the developers have changed the application namespace.
$app->singleton(
'Illuminate\Contracts\Debug\ExceptionHandler',
'Vendor\Package\ExceptionHandler'
);
Otherwise, you might consider providing a trait that a developer can pull into their own ExceptionHandler and leverage your additional functionality that way.

Monolog, log class and method names

I use Monolog 1.10 as logging tool.
Along with the date and loglevel I need to persist class and method names from which logging method has been called. Seems that I need to configure formatter. But I could not find any examples for such case.
How do I do this?
Monolog has Introspection Processor. We need just add it to our logger or handler - and all needed information will be logged
https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/IntrospectionProcessor.php

How do I implement last resort error handling in PHP CodeIgniter?

In web frameworks I've built and used in the past, there's been some means to specify some form of "last resort" error handler. I'd appreciate any help in determining how to accomplish that goal using CodeIgniter, which is a legacy part of a product I'm working on.
The goal of the last resort error handler is to capture any exception that's bubbled up, unhandled, from the application logic. Since, at this high framework level, the handler can't resolve the exception, a typical implementation is to log the error (with associated context) and present a user-friendly error page rather than a scary, technical exception page.
I wasn't able to find support in the CodeIgniter documentation, but I expect there must be support for this. Did I not find support because I should use PHP's set_error_handler() and set_exception_handler()? (I'm new to PHP, but expert in Java, Ruby/Rails.)
Thanks in advance for you guidance!
It seems CodeIgniter 2.0+ registers the handlers you specified to load the CI_Exceptions class. I usually put a MY_Exceptions library in the application/libraries folder to "catch" them before CodeIgniter. I'd rather handle them gracefully than let CI show it's error pages.

Categories