', 'ServiceApi_' . self::getCalledClassShortName() ); return [ // json format : {error: {code: "%code%, message: '%message%'}} 'error' => [ 'code' => (string) $code, 'message' => $message, 'details' => $details, ], ]; } public static function setErrorHandler() { // Disable WP fatal error handler for this request only (scoped via filter). if ( ! has_filter( 'wp_fatal_error_handler_enabled', '__return_false' ) ) { add_filter( 'wp_fatal_error_handler_enabled', '__return_false', 1000 ); } set_error_handler( function ($errno, $errstr, $errfile, $errline) { return static::errorHandler($errno, $errstr, $errfile, $errline); }); register_shutdown_function( function () { static::errorShutdownHandler(); }); } /** * Used to replace the default PHP error handler. * * @param int $errno contains the level of the error * @param string $errstr contains the error message * @param string $errfile errfile, which contains the filename that the error was raised in * @param int $errline errline, which contains the line number the error was raised at * * @return bool Always return true to avoid the default PHP error handler */ public static function errorHandler( $errno, $errstr, $errfile, $errline ) { /* $_errno = $errno & error_reporting(); if ( $_errno === 0 ) { return true; } */ $errorType = [ E_ERROR => 'Error', // handle at shutdown E_WARNING => 'Warning', E_PARSE => 'Parse', // handle at shutdown E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', // handle at shutdown E_CORE_WARNING => 'Core Warning', // handle at shutdown E_COMPILE_ERROR => 'Compile Error', // handle at shutdown E_COMPILE_WARNING => 'Compile Warning', // handle at shutdown E_USER_ERROR => 'Error', E_USER_WARNING => 'User warning', E_USER_NOTICE => 'User notice', E_RECOVERABLE_ERROR => 'Recoverable error', E_DEPRECATED => 'Deprecated notice', E_USER_DEPRECATED => 'Deprecated notice', ]; if ( defined( 'E_STRICT' ) ) { $errorType[ 2048 ] = 'Runtime Notice'; // E_STRICT legacy value } $type = isset( $errorType[ $errno ] ) ? $errorType[ $errno ] : 'Unknown error'; $errorMessage = "[PHP $type level#{$errno}] $errstr ($errfile, line $errline)"; $errorMessage = "[PHP $type level#{$errno}] $errstr ($errfile, line $errline)"; switch ( $errno ) { case E_DEPRECATED: case E_USER_DEPRECATED: break; case E_NOTICE: case E_USER_NOTICE: Logger::log( $errorMessage, 'PHP Notice > ', 'ServiceApi_' . self::getCalledClassShortName(), 0, 1 ); break; case E_WARNING: case E_USER_WARNING: case E_RECOVERABLE_ERROR: Logger::log( $errorMessage, 'PHP Warning > ', 'ServiceApi_' . self::getCalledClassShortName(), 0, 1 ); break; case E_ERROR: case E_PARSE: case E_CORE_ERROR: case E_CORE_WARNING: case E_COMPILE_ERROR: case E_COMPILE_WARNING: case E_USER_ERROR: default: Logger::log( $errorMessage, 'PHP Error > ', 'ServiceApi_' . self::getCalledClassShortName(), 0, 1 ); Helper::send_response_formatted_error( static::makeErrorBody( 'S000', 'Unknown error', $errorMessage ) ); } if ( defined( 'E_STRICT' ) && 2048 === (int) $errno ) { Logger::log( $errorMessage, 'PHP Notice > ', 'ServiceApi_' . self::getCalledClassShortName(), 0, 1 ); } return true; } public static function errorShutdownHandler() { $error_types_to_handle = [ E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, E_USER_ERROR, ]; $last_err = error_get_last(); if ( isset( $last_err['type'] ) && in_array( $last_err['type'], $error_types_to_handle, true ) ) { static::errorHandler( $last_err['type'], $last_err['message'], isset( $last_err['file'] ) ? $last_err['file'] : '', isset( $last_err['line'] ) ? $last_err['line'] : '' ); } } /** * Getting called class name without namespace * * @return string */ public static function getCalledClassShortName() { try { $reflectionClass = new \ReflectionClass( get_called_class() ); return $reflectionClass->getShortName(); } catch ( \Exception $ex ) { return ''; } } }