前言
CI框架的异常处理类通常使用以下几个函数处理错误:
- function show_error($heading, $message, $template = ‘error_general’, $status_code = 500);由于用户操作不当,比如找不到控制器,指定方法之类的,触发的错误。
- function show_404($page = ‘’, $log_error = TRUE);show_error()中的一种特殊情况,表示请求不存在。
- function show_php_error($severity, $message, $filepath, $line);PHP原生错误,来着代码本身的一些错误,例如变量未定义之类的。
- log_exception($severity, $message, $filepath, $line);
我们先来看下Exception类的实现框架:
1 | //Nesting level of the output buffering mechanism |
构造函数 construct()
1 | public function __construct(){ |
记录异常 log_exception($severity, $message, $filepath, $line)
1 | public function log_exception($severity, $message, $filepath, $line){ |
show_404($page = ‘’, $log_error = TRUE)
1 | public function show_404($page = '', $log_error = TRUE){ |
show_error($heading, $message, $template = ‘error_general’, $status_code = 500)
Displays the error message((either as a string or an array)) using the specified template.
1 | public function show_error($heading, $message, $template = 'error_general', $status_code = 500){ |
show_exception($exception)
雷同上个函数,只是获取错误的级别和方式不一样。
1 | public function show_exception($exception){ |
show_php_error($severity, $message, $filepath, $line)
1 | public function show_php_error($severity, $message, $filepath, $line){ |
总结
CI框架的异常处理类文件Exceptions.php,能记录错误日志,将异常信息以既定的模版展示出来。