2

Currently I get an email whenever there is a PHP error in our production application. That works great.

Today a query failed and now I am seeing thousands of "undefined property" errors (all from one page load). This issue is cascading into a problem with sending email because so many error emails are coming to me.

I would like to configure PHP so that "Undefined property" is a fatal error and the script fails at that point. Is that possible?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
William Entriken
  • 37,208
  • 23
  • 149
  • 195
  • Read [this](https://stackoverflow.com/questions/10143172/notice-undefined-property-how-do-i-avoid-that-message-in-php) and after you should find your object. – Aksen P Jan 30 '20 at 15:53
  • can you attach screen shot of pages so i can check that what are issues coming ? – Forge Web Design Jan 30 '20 at 15:53
  • 2
    This is marked duplicate of a question asking "what is an undefined property". That is NOT what I am asking here. – William Entriken Jan 30 '20 at 15:55
  • 1
    @WilliamEntriken The marked duplicate tells you how to properly _prevent_ those messages from happening in the first place. If there's a possibility that something is undefined before you go to use it, you should always check for that case before using it. Upon checking, if you see that it is undefined, you can react however you choose (throw an error message, whatever) – Patrick Q Jan 30 '20 at 15:58
  • 1
    @Patrick Still not what this question is about… This kind of error *can* happen, even if you're super diligent about checking. You overlook one case, and it happens… The question is how to handle this in a specific way *if* it happens. – deceze Jan 30 '20 at 16:01
  • @deceze "You overlook one case, and it happens" I mean, you could say that about anything though. Regardless, if the issue really boils down to not wanting thousands of emails, I feel like the simple solution would be to have an global-level flag indicating whether or not you've sent an error for that page load. Then check the flag before sending an email. Intentionally triggering a fatal error just feels ... dirty. – Patrick Q Jan 30 '20 at 16:20
  • 1
    @PatrickQ you might be using some tools or libraries by others, who were not as pruednt. Prudency before writing the code is not solving the issue of how to handle a code which was already written in a not so prudent manner. – Lajos Arpad Jan 30 '20 at 17:29

2 Answers2

1

Make an error_handler:

function errorHandler($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");

Check the docs here https://www.php.net/manual/en/function.set-error-handler.php

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

Yes, it's possible. Check out the die or exit command. Assuming that you have a single function where you send the email based on the exception, you can modify it so just after sending the email, an if would check whether your criteria to exit the script is fulfilled and if so, call die or exit.

However, I would recommend that instead of crashing the script in this case the errors should enter into a queue of messages, which, when the script ends would be sent as an error digest.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175