We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15083
    • 697 Posts
    Hi there,

    Folks I know its not good practice...and I will probably be attacked for asking...but is there a way in a snippet to ignore E_WARNING, thus allowing a snippet to complete an operation if an E_WARNING is thrown???

    Cheers
      • 22303 MODX Staff
      • 10,725 Posts
      E_NOTICE is already turned off by default in MODx and is controllable for debugging purposes using
      $modx->stopOnNotice= true;

      or
      $modx->stopOnNotice= false;

      as appropriate, which you can see used in index.php.

      In order to stop E_WARNING level errors from being displayed (they shouldn’t be halting script execution btw), you would need to currently `hack` the document.parser file to change the logic in the function phpError() I believe (it only tests and ignores E_NOTICE currently). If you are comfortable with making those kinds of code changes, go for it, but keep in mind that official upgrades and patches will most likely overwrite such `hacks` in core files, but in this case, I don’t think that will be a big inconvenience.

      I’m not sure how the current approach of eval()’ing the snippet code affects this stuff to be honest, so the best way to do it is make some changes and try it out...you might even be able to turn it off in your snippet locally, since it is eval’d wink
        • 15083
        • 697 Posts
        Thanks for the code, you may have guessed from this post that it is related to my other post!!!

        Basically what is happening is this:

        I am including an rss news feed on a site I am making. The code I have works fine until there is a problem with the feed. The feed seems to produce an e_Warning in my script: Bad file descriptor (sometimes).

        for testing purposes I have been forcing errors...to ensure the down feed does not down the site!! And it does.

        If I put in a nonsense URL, The whole page errors as shown below:

        -----------------------------------------------------------------------------------------

        MODx encountered the following error while attempting to parse the requested resource:
        « PHP Parse Error »

        PHP error debug
        Error: fopen(http://www.thebestwebsiteinthewholeentireworld.net/enn.rss1): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
        Error type/ Nr.: Warning - 2

        -----------------------------------------------------------------------------------------

        Now to prevent this I have included the code you gave me from your other post (thank you very much smiley:

        if(!$fp = @fopen($fileCount,’r’)) {
        return "News Unavailable from: ".$fileCount;
        }

        However even turning off the warnings as you suggested has not stopped the whole thing bombing out, which is obviously a major concern as I cannot rely on an rss feed being up!!!!

        Any ideas?
          • 7545
          • 10 Posts
          I know this is a little late, but I recently ran into a similar problem. I figured I would share what I did for anyone who may come across this post like I did. I have modified(hacked) the original core code, so this may not be for everybody, but it is a pretty simple solution.

          After investigating how MODx generates those error messages, I found in /manager/includes/document.parser.class.inc.php that MODx uses a set_error_handler() function which is why setting error_reporting(0) does nothing. The error handling function is called phpError() and is actually a method of the DocumentParser class. The key paramter to look at in this method is $nr which is actually the error number generated when PHP hits an error. By default, when an error occurs, the MODx error page is always displayed UNLESS the PHP error is of level E_NOTCIE-8 AND $modx->stopOnNotice == false. By default, $modx->stopOnNotice = false, but when fopen() fails an error level E_WARNING-2 is generated, same with file_get_contents(). So the error page still shows up. Here is the original phpError() method:
          function phpError($nr, $text, $file, $line) {
              if($nr==8 && $this->stopOnNotice==false) {
                return true;
              }
              if (is_readable($file)) {
                $source = file($file);
                $source = htmlspecialchars($source[$line-1]);
              } else {
                $source = "";
              }  //Error $nr in $file at $line: <div><code>$source</code></div>
              $this->messageQuit("PHP Parse Error", '', true, $nr, $file, $source, $text, $line);
            }
          


          So what I did was edit this method to ignore E_WARNING errors along with E_NOTICE. Just change the first IF statement in phpError() to
          if(($nr==8 || $nr == 2) && $this->stopOnNotice==false)

          This seems to fix it for my needs anyway. I’d prefer not to edit the core code, but this was the only option I could think of. I think using @ before fopen() doesn’t work because that just supresses the error message from displaying, but the error is still generated internally I think. Anyway, thats all you need to do - edit /manager/includes/document.parser.class.inc.php and overwite the one on the server. Also, make sure you have $modx->stopOnNotice == false in your index.php.

          P.S. - It would be nice for future MODx releases to have an option in the admin panel to choose what error levels you want to be ignored.

          -Chris O.