Create error handler service
@param Di $di
@return \Phalex\Mvc\Exception\HandlerDefault
@throws InvalidArgumentException
426331
public function createService(Di $di)
    {
        $required = [
            'views_dir'    => null,
            'template_500' => null,
            'template_404' => null,
        ];

        $config = $di->get('config')['error_handler']->toArray();
        $errMsg = sprintf('Cannot create error handler "%s"', __CLASS__);
        if (!isset($config['options'])) {
            throw new InvalidArgumentException($errMsg);
        }

        $options  = array_merge($required, $config['options']);
        if (empty($options['views_dir']) || empty($options['template_500']) || empty($options['template_404']) ||
                ($realPath = realpath($options['views_dir'])) === false) {
            throw new InvalidArgumentException($errMsg);
        }

        $this->options  = $options;
        $this->di       = $di;
        $this->viewsDir = $realPath;
        return $this;
    }
270899
public function getErrorProcessor(): MiddlewareInterface
    {
        if (is_null($this->errorProcessor)) {
            if (method_exists($this, 'resolve')) {
                $this->setErrorProcessor($this->resolve(Middleware::class));
            } else {
                $this->setErrorProcessor(new Middleware());
            }
        }

        return $this->errorProcessor;
    }
1285459
private function _exceptionHandler($e)
    {
        $this->_errorController->setParams(array('e' => $e));
        $action = FrontController::EXCEPTION_HANDLER_ACTION . 'Action';
        $this->_response->setStatusCode(500);

        return $this->_errorController->$action();
    }
536931
public function error($exception)
    {
        $response = new Response();

        // Set the default error response
        $response->setContent('There has been a problem serving this request.');
        $response->headers->set('content-type', 'text/html');
        $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);

        // Execute the user defined error callback
        if (is_callable($this->error)) {
            $response = call_user_func($this->error, $response, $exception);
        }

        // Send the response
        $response->send();
    }
1065674
private function createErrorHandler()
    {
        /**
         * @param int $errno
         * @param string $errstr
         * @param string $errfile
         * @param int $errline
         * @return void
         * @throws \ErrorException if error is not within the error_reporting mask.
         */
        return function ($errno, $errstr, $errfile, $errline) {
            if (! (error_reporting() & $errno)) {
                // error_reporting does not include this error
                return;
            }

            throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
        };
    }
791131
public function addFatalErrorHandler($handler) {
        if ( ! $handler instanceof INativeErrorHandler && ! is_callable($handler)) {
            throw new InvalidHandlerType(
                s('Fatal error handler must be a callable or an instance of %s.',
                    INativeErrorHandler::class)
            );
        }

        if (is_callable($handler)) {
            $handler = $this->createNativeErrorHandler($handler);
        }

        $this->fatalErrorHandlers[] = $handler;
    }
480887
public static function register(self $handler = null, $replace = true)
    {
        if (null === self::$reservedMemory) {
            self::$reservedMemory = str_repeat('x', 10240);
            register_shutdown_function(__CLASS__.'::handleFatalError');
        }

        if ($handlerIsNew = null === $handler) {
            $handler = new static();
        }

        if (null === $prev = set_error_handler(array($handler, 'handleError'))) {
            restore_error_handler();
            // Specifying the error types earlier would expose us to https://bugs.php.net/63206
            set_error_handler(array($handler, 'handleError'), $handler->thrownErrors | $handler->loggedErrors);
            $handler->isRoot = true;
        }

        if ($handlerIsNew && is_array($prev) && $prev[0] instanceof self) {
            $handler = $prev[0];
            $replace = false;
        }
        if ($replace || !$prev) {
            $handler->setExceptionHandler(set_exception_handler(array($handler, 'handleException')));
        } else {
            restore_error_handler();
        }

        $handler->throwAt(E_ALL & $handler->thrownErrors, true);

        return $handler;
    }
1051820
public function addExceptionHandler($handler) {
        if ( ! $handler instanceof IExceptionHandler && ! is_callable($handler)) {
            throw new InvalidHandlerType(
                s('Exception handler must be a callable or an instance of %s.',
                    IExceptionHandler::class)
            );
        }

        if (is_callable($handler)) {
            $handler = $this->createExceptionHandler($handler);
        }

        $this->exceptionHandlers[] = $handler;
    }
1333691
protected static function handleError()
	{
		if(self::$errorHandler === NULL)
		{
			self::$errorHandler = function ($type, $message, $file, $line) {
				throw new \RuntimeException($message);
			};
		}
		
		return self::$errorHandler;
	}
254794
public function get($id)
    {
        try {
            $handler = $this->resolve($id);

            if ($handler instanceof RequestHandlerInterface) {
                return $handler;
            }

            return new CallableHandler($handler);
        } catch (NotFoundExceptionInterface $exception) {
            throw $exception;
        } catch (Exception $exception) {
            throw new class("Error getting the handler $id", 0, $exception) extends Exception implements
                ContainerExceptionInterface {
            };
        }
    }
742821
public function getHandlerType(ContainerInterface $di = null) : string
    {
        $handler = new RouteHandler();
        return $handler->getHandlerType($this->handler, $di);
    }
1163645
public function getErrorHandler() : ErrorHandlerInterface
    {
        if (!$this->errorHandler) {
            $this->errorHandler = new ErrorHandler(
                (bool) $this->getConfiguration('displayErrors')
            );
        }

        return $this->errorHandler;
    }
1083525
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $request = $request->withAttribute(self::ORIGINAL_REQUEST, $request);

        set_error_handler($this->createErrorHandler());

        try {
            $response = $handler->handle($request);
        } catch (Throwable $exception) {
            $response = $this->handleThrowable($exception, $request);
        }

        restore_error_handler();

        return $response;
    }
677677
protected function get_default_handler( $dependency ) {
		switch ( $dependency ) {
			case self::KEY_STYLES:
				return self::DEFAULT_STYLE_HANDLER;
			case self::KEY_SCRIPTS:
				return self::DEFAULT_SCRIPT_HANDLER;
			default:
				return null;
		}
	}
692609
public function handle(
        string $path = null,
        ContainerInterface $di = null
    ) {
        if ($this->mount) {
            // Remove the mount path to get base for controller
            $len = strlen($this->mount);
            if (substr($path, 0, $len) == $this->mount) {
                $path = ltrim(substr($path, $len), "/");
            }
        }

        try {
            $handler = new RouteHandler();
            return $handler->handle($this->methodMatched, $path, $this->handler, $this->arguments, $di);
        } catch (ConfigurationException $e) {
            throw new ConfigurationException(
                $e->getMessage()
                . " Route matched method='{$this->methodMatched}', mount='{$this->mount}', path='$path', handler='"
                . (
                    is_string($this->handler)
                        ? "$this->handler"
                        : "array/object"
                )
                . "'."
            );
        }
    }
731932
private function defaultErrorHandler(
        ServerRequestInterface $request,
        ResponseInterface $response
    ) {
        $response = $response->withStatus(422, 'Unprocessable Entity')
            ->withHeader('Content-Type', 'application/json');

        $middlewareAttribute = $request->getAttribute(Middleware::KEY, []);

        if (isset($middlewareAttribute[self::KEY])) {
            /** @var ResponseInterface $response */
            $stream = $response->getBody();
            $stream->write(json_encode($middlewareAttribute[self::KEY], JSON_UNESCAPED_SLASHES));
        }

        return $response;
    }
1205198
protected function makeClosure($handler)
    {
        if (is_callable($handler)) {
            return $handler;
        }

        if (is_string($handler)) {
            if (!class_exists($handler)) {
                throw new InvalidArgumentException(sprintf('Class "%s" not exists.', $handler));
            }

            if (!in_array(EventHandlerInterface::class, (new \ReflectionClass($handler))->getInterfaceNames(), true)) {
                throw new InvalidArgumentException(sprintf('Class "%s" not an instance of "%s".', $handler, EventHandlerInterface::class));
            }

            return function ($payload) use ($handler) {
                return (new $handler($this->app ?? null))->handle($payload);
            };
        }

        if ($handler instanceof EventHandlerInterface) {
            return function () use ($handler) {
                return $handler->handle(...func_get_args());
            };
        }

        throw new InvalidArgumentException('No valid handler is found in arguments.');
    }