承接 youssef/error 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

youssef/error

Composer 安装命令:

composer require youssef/error

包简介

Makes handling and debugging PHP errors suck less.

README 文档

README

Makes handling and debugging PHP errors suck less. Based on kuria/error (https://github.com/kuria/error)

Table of Contents

Features

  • debug and non-debug mode
  • converts PHP errors (warnings, notices, etc) into exceptions
    • respects the global error_reporting setting
  • handles uncaught exceptions and fatal errors (including parse errors)
  • CLI error screen (writes errors to STDERR)
  • web error screen (renders errors for web browsers)
    • non-debug mode:
      Web error screen in non-debug mode
      • simple error message
      • does not disclose any internal information
      • does not use any variation of the word "oops"
    • debug mode:
      Web error screen in debug mode
      • file paths and line numbers
      • highlighted code previews
      • stack traces
      • argument lists
      • variable contexts
      • output buffer (can be shown as HTML too)
      • plaintext trace (for copy-paste)
  • event system that can be utilised to:
    • implement logging
    • suppress or force errors conditionally
    • change or add content to the error screens

Requirements

  • PHP 5.3 or newer

Usage example

use Kuria\Error\ErrorHandler;

$debug = true; // true during development, false in production
error_reporting(E_ALL | E_STRICT); // configure the error reporting

$errorHandler = new ErrorHandler($debug);
$errorHandler->register();

// trigger an error to see the error handler in action
echo $invalidVariable;

Event system

  • implemented using the kuria/event library
  • the error handler fires events as it handles errors
  • both built-in error screen implementations emit events as they render

Error handler events

Possible events emitted by the ErrorHandler class:

error

  • emitted when a PHP errors occurs
  • arguments:
    1. object $exception
      • instance of ErrorException or Kuria\Error\ContextualErrorException
    2. bool $debug
    3. bool &$suppressed
      • reference to the suppressed state of the error
      • the error can be suppressed by current error_reporting configuration or by other event handlers

fatal

  • emitted when an uncaught exception or a fatal error is being handled
  • arguments:
    1. object $exception
    2. bool $debug
    3. FatalErrorHandlerInterface &$handler
      • reference to the current fatal error handler

emerg

  • emitted when another exceptions has been thrown during fatal error handling
  • more uncaught exceptions or a fatal error at this point will just kill the script
  • arguments:
    1. object $exception
    2. bool $debug

Web error screen events

Possible events emitted by the WebErrorScreen class:

render

  • emitted when rendering in non-debug mode
  • arguments:
    1. array &$view
      • title: used in <title>
      • heading: used in <h1>
      • text: content of the default paragraph
      • extras: custom HTML after the main section
        • append to this using .=
    2. object $exception
    3. string|null $outputBuffer
    4. WebErrorScreen $screen

render.debug

  • emitted when rendering in debug mode
  • arguments:
    1. array &$view
      • title: used in <title>
      • extras: custom HTML after the main section
        • append to this using .=
    2. object $exception
    3. string|null $outputBuffer
    4. WebErrorScreen $screen

layout.css

  • emitted when CSS styles are being output
  • arguments:
    1. string &$css
      • reference to the CSS output
      • it can be appended to or replaced
    2. bool $debug
    3. WebErrorScreen $screen

layout.js

  • emitted when JavaScript code is being output
  • arguments:
    1. string &$js
      • reference to the JavaScript output
      • it can be appended to or replaced
    2. bool $debug
    3. WebErrorScreen $screen

CLI error screen events

Possible events emitted by the CliErrorScreen class:

render

  • emitted when rendering in non-debug mode
  • arguments:
    1. array &$view
      • title: first line of output
      • output: error message
        • you can append to it using .=
    2. object $exception
    3. string|null $outputBuffer
    4. CliErrorScreen $screen

render.debug

  • emitted when rendering in debug mode
  • arguments:
    1. array &$view
      • title: first line of output
      • output: exception information
        • you can append to it using .=
    2. object $exception
    3. string|null $outputBuffer
    4. CliErrorScreen $screen

Event listener examples

Notes

  • do not typehint the Exception class in your listeners if you want to be compatible with the new exception hierarchy in PHP 7

Logging

Logging unhandled errors into a file.

use Kuria\Error\Util\Debug;

$errorHandler->on('fatal', function ($exception, $debug) {
    $logFilePath = sprintf('./errors_%s.log', $debug ? 'debug' : 'prod');

    $entry = sprintf(
        "[%s] %s - %s in file %s on line %d\n",
        date('Y-m-d H:i:s'),
        Debug::getExceptionName($exception),
        $exception->getMessage(),
        $exception->getFile(),
        $exception->getLine()
    );

    file_put_contents($logFilePath, $entry, FILE_APPEND | LOCK_EX);
});

Disabling the "@" operator

This listener causes statements like echo @$invalidVariable; to throw an exception regardless of the "shut-up" operator.

$errorHandler->on('error', function ($exception, $debug, &$suppressed) {
    $suppressed = false;
});

Altering the error screens

Examples for the web error screen.

Changing default labels of the non-debug error screen:

use Kuria\Error\Screen\WebErrorScreen;

$errorHandler->on('fatal', function ($exception, $debug, $screen) {
   if (!$debug && $screen instanceof WebErrorScreen) {
        $screen->on('render', function (&$view) {
            $view['heading'] = 'It is all your fault!';
            $view['text'] = 'You have broken everything and now I hate you.';
        });
    }
});

Adding customized section to the debug screen:

use Kuria\Error\Screen\WebErrorScreen;

$errorHandler->on('fatal', function ($exception, $debug, $screen) {
   if ($debug && $screen instanceof WebErrorScreen) {
        $screen
            ->on('layout.css', function (&$css) {
                $css .= '#custom-group {color: #f60000;}';
            })
            ->on('render.debug', function (&$view) {
                $view['extras'] .= <<<HTML
<div id="custom-group" class="group">
    <div class="section">
        Example of a custom section
    </div>
</div>
HTML;
            })
        ;
    }
});

youssef/error 适用场景与选型建议

youssef/error 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「debug」 「error handler」 「exception handler」 「exception」 「error」 「debugger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 youssef/error 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 youssef/error 我们能提供哪些服务?
定制开发 / 二次开发

基于 youssef/error 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-14