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

    I’m currently developing a Snippet (which will be made available to the community once it’s done). There are several cases where i need to throw an exception in case of an error.
    My current idea is to let the class throw exceptions and then catch them in the snippet code where the class is being instanciated.
    I wonder what to do with that catched exception though... how should i "pass" the error to modx? What to do to make errors appear in the MODx event-log?

    Thanks for any hints.

    Edit:
    Another Question that came to my mind: If using the modx db API, are parameters passed to these functions being sanitized? Or does the user have to take care of that himself?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Second question, yes the user has to sanitize the input. $modx->db->escape() is a wrapper for mysql_real_escape_string if that’s available, and mysql_escape_string if it’s not.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 2734
        • 165 Posts
        Hi sottwell

        Thanks for the reply.
        To answer my first question myself: I guess i could use the $modx->messageQuit() method for that matter?

        Edit:
        I’m using $modx->messageQuit(); for now. Thread closed. Unless somebody has a better solution of course smiley
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Yes, that will work; it’s a bit extreme at times.

          You can just create an error reporting function in your snippet, really all the messageQuit does is determine what message to display, dresses it up in its own HTML, echos it and logs it, then exits. Here’s the end of the function:
                  echo $parsedMessageString;
                  ob_end_flush();
          
                  // Log error
                  $this->logEvent(0, 3, $parsedMessageString, $source= 'Parser');
          
                  // Make sure and die!
                  exit();
              }
          

          The whole function is near the end of the document.parser.class.inc.php file.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org