Shudan Wang

没有人不爱惜他的生命,但很少人珍视他的时间。

  • 主页
  • 随笔
所有文章 友链 关于我

Shudan Wang

没有人不爱惜他的生命,但很少人珍视他的时间。

  • 主页
  • 随笔

CodeIgniter框架源码解析十七之异常处理类文件Exceptions.php

2018-04-09

前言

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Nesting level of the output buffering mechanism
public $ob_level;
//List of available error levels
public $levels = array(
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
);

public function __construct(){...}
public function log_exception($severity, $message, $filepath, $line){...}
public function show_404($page = '', $log_error = TRUE){...}
public function show_error($heading, $message, $template = 'error_general', $status_code = 500){...}
public function show_exception($exception){...}
public function show_php_error($severity, $message, $filepath, $line){...}

构造函数 construct()

1
2
3
4
public function __construct(){
$this->ob_level = ob_get_level();
// Note: Do not log messages from this constructor.
}

记录异常 log_exception($severity, $message, $filepath, $line)

1
2
3
4
public function log_exception($severity, $message, $filepath, $line){
$severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
}

show_404($page = ‘’, $log_error = TRUE)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public function show_404($page = '', $log_error = TRUE){
if (is_cli()){
$heading = 'Not Found';
$message = 'The controller/method pair you requested was not found.';
}else{
$heading = '404 Page Not Found';
$message = 'The page you requested was not found.';
}
// By default we log this, but allow a dev to skip it
if ($log_error){
log_message('error', $heading.': '.$page);
}
echo $this->show_error($heading, $message, 'error_404', 404);
exit(4); // EXIT_UNKNOWN_FILE
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public function show_error($heading, $message, $template = 'error_general', $status_code = 500){
$templates_path = config_item('error_views_path');
if (empty($templates_path)){
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
}

if (is_cli()){
$message = "\t".(is_array($message) ? implode("\n\t", $message) : $message);
$template = 'cli'.DIRECTORY_SEPARATOR.$template;
}else{
set_status_header($status_code);
$message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>';
$template = 'html'.DIRECTORY_SEPARATOR.$template;
}

if (ob_get_level() > $this->ob_level + 1){
//如果是在视图文件发生错误,则就会出现缓冲级别相差1的情况,此时先把输出的内容给flush出来,然后再把错误信息输出。
ob_end_flush();
}
ob_start();
include($templates_path.$template.'.php');
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}

show_exception($exception)

雷同上个函数,只是获取错误的级别和方式不一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public function show_exception($exception){
$templates_path = config_item('error_views_path');
if (empty($templates_path)){
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
}

$message = $exception->getMessage();
if (empty($message)){
$message = '(null)';
}

if (is_cli()){
$templates_path .= 'cli'.DIRECTORY_SEPARATOR;
}else{
$templates_path .= 'html'.DIRECTORY_SEPARATOR.$template;
}

if (ob_get_level() > $this->ob_level + 1){
ob_end_flush();
}
ob_start();
include($templates_path.'error_exception.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}

show_php_error($severity, $message, $filepath, $line)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function show_php_error($severity, $message, $filepath, $line){
$templates_path = config_item('error_views_path');
if (empty($templates_path)){
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
}

$severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
// For safety reasons we don't show the full file path in non-CLI requests,最后两段路径信息
if ( ! is_cli()){
$filepath = str_replace('\\', '/', $filepath);
if (FALSE !== strpos($filepath, '/')){
$x = explode('/', $filepath);
$filepath = $x[count($x)-2].'/'.end($x);
}
$template = 'html'.DIRECTORY_SEPARATOR.'error_php';
}else{
$template = 'cli'.DIRECTORY_SEPARATOR.'error_php';
}

if (ob_get_level() > $this->ob_level + 1){
ob_end_flush();
}
ob_start();
include($templates_path.$template.'.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}

总结

CI框架的异常处理类文件Exceptions.php,能记录错误日志,将异常信息以既定的模版展示出来。

赏

谢谢你请我吃糖果

支付宝
微信
  • CI框架源码解析

扫一扫,分享到微信

微信分享二维码
CodeIgniter框架源码解析十八之日志记录类文件Log.php
Mac的Nginx+php-fpm+php5.6.3环境下升级为php7.2.4
  1. 1. 前言
  2. 2. 构造函数 construct()
  3. 3. 记录异常 log_exception($severity, $message, $filepath, $line)
  4. 4. show_404($page = ‘’, $log_error = TRUE)
  5. 5. show_error($heading, $message, $template = ‘error_general’, $status_code = 500)
  6. 6. show_exception($exception)
  7. 7. show_php_error($severity, $message, $filepath, $line)
  8. 8. 总结
© 2023 Shudan Wang
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • CI框架源码解析
  • HTTP原理
  • 网络技术
  • Laravel
  • Linux基础篇
  • Linux之常用命令
  • php杂集
  • 随笔
  • PHP
  • 网络安全
  • GO
  • mysql
  • Apache
  • Git
  • SSH
  • V2Ray
  • 数据结构
  • HTML
  • Nginx
  • NoSQL
  • Docker
  • Memcached

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 廖雪峰
  • 阮一峰
  • 陈皓
很惭愧

只做了一点微小的工作
谢谢大家