php ci try catch,CodeIgniter3.0+框架自定义异常处理实现

背景

ci3.0框架核心代码自动实现了异常,并实现了抛出的对应页面和方法,对于一些个性化需求特别是接口类型的应用,会不合适。因此需要在不改版核心代码 (system目录下文件),来改变对异常及404等相关异常的处理。

方法说明

ci框架3.0比2.0有比较大的改动,其中之一就是对异常的处理。以下是CodeIgniter-3.1.8\system\core\CodeIgniter.php中对异常处理的部分代码

/*

* ------------------------------------------------------

* Define a custom error handler so we can log PHP errors

* ------------------------------------------------------

*/

set_error_handler('_error_handler');

set_exception_handler('_exception_handler');

register_shutdown_function('_shutdown_handler');

...

以上括号内的方法均在common.php中以function_exists为前提声明。

...

if ( ! function_exists('_exception_handler'))

{

...

代码实现

我们简单粗暴的在项目入口文件index.php中重写以下方法

/**

* 推送到redis cc异常队列

* @time 2019/3/21 15:29

* @author tongbo

* @param $msg

* @param $error_type

* @param $error_code

* @return bool|int|string

*/

function redis_list_add($msg, $error_type, $error_code)

{

ini_set('default_socket_timeout', -1);

$v = explode(':', $_SERVER['SITE_REDIS_SERVER']);

if (is_array($v) && !empty($v)) {

try {

$redis = new redis();

$redis->pconnect($v[0], $v[1]);

$space = "\n\n";

$trace = "";

if ($error_code) {

$trace .= "状态码 :" . $error_code . "\n";

}

if ($error_type) {

$trace .= "错误类型 :" . $error_type . "\n";

}

$trace .= "客户端IP : " . $_SERVER['REMOTE_ADDR'] . "\n";

$trace .= "服务端IP : " . $_SERVER['SERVER_NAME'] . "(" . $_SERVER['SERVER_ADDR'] . ")" . "\n";

$trace .= "请求地址 : " . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : "empty") . "\n";

$trace .= "请求参数 : =" . print_r($_REQUEST ? $_REQUEST : $_GET, true) . "\n";

return $redis->LPUSH('CC_PHP_ERROR_WARNING', $space . $msg . $space . $trace);

} catch (Exception $e) {

return $e->getMessage();

}

}

}

/**

* 优先重写common.php中对应方法

* 捕捉语法错误

* @time 2019/3/21 16:19

* @author tongbo

* @param $severity

* @param $message

* @param $filepath

* @param $line

*/

function _error_handler($severity, $message, $filepath, $line)

{

$is_error = (((E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR | E_STRICT) & $severity) === $severity);

if ($is_error) {

$error_msg = $message . "\n" . $filepath . "\n" . $line;

$error_code = "501";

redis_list_add($error_msg, 'Error_Handler异常', $error_code);

exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'error']));

}

}

/**

* 捕获php本身语法,对象调用,参数类型传递等错误

* 优先重写common.php中对应方法

* ParseError,object(Error),TypeError,Error

* @time 2019/3/20 18:33

* @author tongbo

* @param $exception

*/

function _exception_handler($exception)

{

if (!empty($exception)) {

$error_type = get_class($exception);

$error_msg = "

错误类型: {$error_type};

文件名: {$exception->getFile()};

所在行号: {$exception->getLine()};

错误消息: {$exception->getMessage()}; ";

$error_code = "502";

redis_list_add($error_msg, $error_type, $error_code);

exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'exception']));

}

}

/**

* 优先重写common.php中对应方法

* require_once('no_exists.php')

* @time 2019/3/21 9:49

* @author tongbo

*/

function _shutdown_handler()

{

$last_error = error_get_last();

if (isset($last_error) &&

($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) {

$error_msg = "

错误类型: shutdown; >

文件名: {$last_error['file']};

所在行号: {$last_error['line']};

错误消息: {$last_error['message']}; ";

$error_code = "503";

redis_list_add($error_msg, 'Shut_Down异常', $error_code);

exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'shutdown']));

}

}

/**

* 优先重写common.php中对应方法

* ci 框架内部的load异常、config异常、loader异常等会自动抛出,

* 但common.php中的函数定义之类错误无法捕捉

* @time 2019/3/20 18:46

* @author tongbo

* @param $message

* @param int $status_code

*/

function show_error($message)

{

$error_msg = "错误消息: {$message}; ";

$error_code = "504";

redis_list_add($error_msg, '框架加载异常', $error_code);

exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'ci_exception_1']));

}

/**

* 优先重写common.php中对应方法

* @time 2019/3/21 15:34

* @author tongbo

* @param string $page

*/

function show_404($page = '')

{

$error_msg = "错误消息: 请求的URL: " . $page . " 404 not found";

$error_code = "404";

redis_list_add($error_msg, '文件不存在', $error_code);

exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'Not Found']));

}

延伸

在基类中可以处理

错误等级区分对待

将错误处理写到其他地方,require_once进来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值