异常处理


方式一

<?php

use Timo\Core\Engine;
use Timo\Exception\CoreException;

define('APP_NAME', 'api');
define('ENV', $_SERVER['HTTP_HOST'] == 'api.timoph.com' ? 'pro' : ($_SERVER['HTTP_HOST'] == 't.api.timoph.com' ? 'test' : 'dev'));
define('APP_DEBUG', ENV == 'dev');
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);

require ROOT_PATH . 'vendor/autoload.php';
require ROOT_PATH . '../TimoPHP/boot.php';

try {
    $engine = new Engine();
    $engine->start();
} catch (Exception $e) {
    CoreException::handle($e);
} catch (Error $e) {
    CoreException::handle($e);
}

方式二,自己来处理异常

增加一个Error.php控制器

<?php

use Timo\Core\Engine;
use Timo\Exception\CoreException;

define('APP_NAME', 'api');
define('ENV', $_SERVER['HTTP_HOST'] == 'api.timoph.com' ? 'pro' : ($_SERVER['HTTP_HOST'] == 't.api.timoph.com' ? 'test' : 'dev'));
define('APP_DEBUG', ENV == 'dev');
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);

require ROOT_PATH . 'vendor/autoload.php';
require ROOT_PATH . '../TimoPHP/boot.php';

// 异常处理
try {
    $engine->start();
} catch(Exception $e) {

    //跳转到404页面,异常处理和日志记录
    $engine->run('Error', '_404', ['e' => $e]);
} catch(Error $e) {

    $engine->run('Error', '_404', ['e' => $e]);
}
$engine->run('Error', '_404', ['e' => $e]);
Error就是Error控制器,_404就是操作,这个是自定义的,你可以随意更改控制器和操作,比如改为Abc控制器的def操作
把异常记录到/logs/Exception/下面
如:/logs/Exception/08.10.log