<?php


namespace App\Exception;

use Api\PhpUtils\Log\FileLog;

class ExceptionErrorCatch
{
    public static function register()
    {
        error_reporting(E_ALL & ~E_NOTICE);
        set_exception_handler([__CLASS__, 'appException']);
        set_error_handler([__CLASS__, 'appError'], E_ALL);
    }

    public static function appException(\Throwable $e)
    {
        $handler = new ExceptionHandler();
        $handler->render($e);
        exit;
    }

    public static function appError($errno, $errstr, $errfile, $errline)
    {
        if ($errno === E_WARNING || $errno === E_NOTICE) {
            $prefix = $errno === E_WARNING?'PHP Warning:  ':'PHP Notice:  ';
            error_log($prefix.'['.$errstr.']'.' [detail info:] '.' in '.$errfile.' on line '.$errline.' '.json_encode(debug_backtrace(false)).' [request info:] '.FileLog::accessLog());
            return true;
        }else if ($errno === E_USER_ERROR) {
            $prefix = 'PHP E_USER_ERROR:  ';
            error_log($prefix.'['.$errstr.']'.' [detail info:] '.' in '.$errfile.' on line '.$errline.' '.json_encode(debug_backtrace(false)).' [request info:] '.FileLog::accessLog()());
        }
        return false;
        // $handler = new ErrorHandler();
        // $handler->render($errno, $errstr, $errfile, $errline);
        // exit;
    }
}