<?php


namespace App\Exception;

/*
 * 重写Handle的render方法，实现自定义异常消息
 */

use Api\PhpUtils\Log\FileLog;
use Helpers\ApiResponse;
use Yaf\Registry;

class ErrorHandler
{
    use ApiResponse;

    private $http_code;
    private $msg;
    private $code;

    public function render($errno, $errstr, $errfile, $errline)
    {
        $msg = ' file_name : '.$errfile . '<br> line_num : '.$errline . '<br> message : ' . $errstr;
        if(_IS_DEBUG){
            echo $msg;
            exit;
        }

        $this->msg  = appConfig('exception.sys.msg');
        $this->code = appConfig('exception.sys.code');

        $this->recordErrorLog($msg);

        if(PHP_SAPI != 'cli') {
            $this->failed($this->code, $this->msg);
        }
    }

    /*
     * 将异常写入日志
     */
    private function recordErrorLog($msg)
    {
        FileLog::error('Error handler', $msg);
    }
}