Handling errors on Modx

Hi, we are moving onto production, and need to make sure we are not showing any weird error to our end users, specially fatal errors, already tried adding the following to my MODx.class but the error is still shown, what I’m missing?

/**
 * Loads the error handler for this instance.
 *
 * @param array|null $options An array of options for the errorHandler.
 */
 protected function _initErrorHandler($options = null) {
 	if ($this->errorHandler == null || !is_object($this->errorHandler)) {
        if ($ehClass = $this->getOption('error_handler_class', $options, 'modErrorHandler', true)) {
            $ehPath = $this->getOption('error_handler_path', $options, '', true);
            if ($ehClass = $this->loadClass($ehClass, $ehPath, false, true)) {
                if ($this->errorHandler = new $ehClass($this)) {
                    // $result = set_error_handler(array ($this->errorHandler, 'handleError'), $this->getOption('error_handler_types', $options, error_reporting(), true));
                    $result = set_error_handler('myErrorHandler');

	        if ($result === false) {
	            $this->log(modX::LOG_LEVEL_ERROR, 'Could not set error handler.  Make sure your class has a function called handleError(). Result: ' . print_r($result, true));
	        }
	        set_exception_handler('exception_handler');
	        register_shutdown_function("fatal_handler");
	        $this->log(modX::LOG_LEVEL_ERROR, 'Exc handler set1! ');
                }
            }
        }
    }
}

/**
* Simple error handling method
**/
function exception_handler($exception) {
	echo "Uncaught exception: " , $exception->getMessage(), "\n";
	$this->log(modX::LOG_LEVEL_ERROR, 'Non managed exepction found! ');
	return true;
}

function fatal_handler() {
    $errfile = "unknown file";
    $errstr  = "shutdown";
    $errno   = E_CORE_ERROR;
    $errline = 0;

    $error = error_get_last();
    $this->log(modX::LOG_LEVEL_ERROR, 'Fatal!!!');

    if($error !== NULL) {
        $errno   = $error["type"];
        $errfile = $error["file"];
        $errline = $error["line"];
        $errstr  = $error["message"];
    echo "Uncaught exception: " , $errstr, "\n";
	}
	$this->log(modX::LOG_LEVEL_ERROR, 'Non managed exepction found! ');    	
	return true;
}

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    echo "<b>Custom error:</b> [$errno] $errstr<br>";
    echo " Error on line $errline in $errfile<br>";
	$this->log(modX::LOG_LEVEL_ERROR, 'Non managed exepction found! ');        
    return true;
}

Editing core files is a very bad idea and not something I’d ever suggest putting in production.

What error are you seeing?

2 Likes

Hi there, and thanks for your answer, it pointed me in the right direction. In case anyone else is as lost as I’m inside MODx, finally, was a matter of creating a snippet like this

register_shutdown_function(
function() {
$error = error_get_last();
if (is_array($error)) {
$errorCode = $error[‘type’] ?? 0;
$errorMsg = $error[‘message’] ?? ‘’;
$file = $error[‘file’] ?? ‘’;
$line = $error[‘line’] ?? null;

          if ($errorCode > 0) {
              // handle_error($errorCode, $errorMessage, $file, $line);
              // echo errorCode . ': '. $errorMessage . 'in ' . $file . 'on line pendejo!' . $line;
              echo "El sitio se totio aca toca poner codigo decente!!";
          }
      }
  });

And have this called as part of the head, I’m not breaking anything by doing it like this, right?