jasny/error-handler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

jasny/error-handler

最新稳定版本:v0.2.0

Composer 安装命令:

composer require jasny/error-handler

包简介

Error handler with PSR-7 support

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight Packagist Stable Version Packagist License

Error handler with PSR-7 support.

Installation

The Jasny Error Handler package is available on packagist. Install it using composer:

composer require jasny/error-handler

Usage

$errorHandler = new Jasny\ErrorHandler();

Just creating an error handler will do nothing. You can use it for logging, handling fatal errors and as PSR-7 compatible middleware.

Logging

By default the error handler with only catch Throwables and not set the php error handler.

To log errors, set the logger using setLogger(). You can log with any PSR-3 compatible logger like Monolog.

The logUncaught() method will set the error handler, so warnings and notices can be logged. It may also register a shutdown function to handle uncatchable fatal errors.

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$errorHandler = new Jasny\ErrorHandler();

$log = new Logger('test');
$log->pushHandler(new StreamHandler('path/to/your.log'));

// Log fatal errors, warnings and uncaught exceptions
$errorHandler->setLogger($log);

$errorHandler->logUncaught(E_PARSE | E_ERROR | E_WARNING | E_USER_WARNING);
$errorHandler->logUncaught(Exception::class);
$errorHandler->logUncaught(Error::class); // PHP7 only

PSR-7 compatible middleware

The error handler can be used as PSR-7 compatible (double-pass) middleware.

The error will catch Exceptions and Errors.

You can use this middleware with:

For example use it with Relay:

use Relay\RelayBuilder;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;

$errorHandler = new Jasny\ErrorHandler();

$relay = new RelayBuilder();
$dispatcher = $relay->newInstance([$errorHandler->asMiddleware()]);

$response = $dispatcher((new ServerRequest())->withGlobalEnvironment(), new Response());

Or with Jasny Router:

use Jasny\Router;
use Jasny\Router\Routes\Glob as Routes;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;

$router = new Router(new Routes(['/**' => ['controller' => '$1', 'id' => '$2']));

$errorHandler = new Jasny\ErrorHandler();
$router->add($errorHandler->asMiddleware());

$response = $dispatcher((new ServerRequest())->withGlobalEnvironment(), new Response());

PHP 5 support

With PHP 5 errors aren't thrown, so the middleware won't handle it. To add middleware support for errors in PHP5, you should call converErrorsToExceptions(). This method will convert an error to an ErrorException.

Handling fatal errors

Errors that are not thrown, like syntax errors, are not caught and will cause a fatal error. With the logUncaught() method, you can specify that the error handler should also these kind of errors.

With the onFatalError() method you take additional action, like output a pretty error message.

ob_start();

$errorHandler = new Jasny\ErrorHandler();

$errorHandler->logUncaught(E_ERROR | E_RECOVERABLE_ERROR | E_USER_ERROR);

$errorHandler->onFatalError(function() {
    http_response_code(500);
    header('Content-Type: text/html');
    echo "<h1>An unexpected error occured</h1><p>The error has been logged.</p>";
}, true);

Use true as second argument of onFatalError to the output buffer before calling your function.

Combine with other error handlers

Using the error logger might lose backtrace information that other error handlers can pick up. Jasny Error Handler will always call the previous error handler, including the PHP internal error handler for non-thrown errors.

When using Rollbar you should not use the Rollbar handler for Monolog. By using Rollbar's own error handler, you'll get better error reports:

use Jasny\ErrorHandler;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Rollbar error handler will log uncaught errors
Rollbar::init(array('access_token' => 'POST_SERVER_ITEM_ACCESS_TOKEN'));

$log = new Logger('test');
$log->pushHandler(new RollbarHandler(Rollbar::$instance));

$errorHandler = new ErrorHandler();

// Jasny error handler will only log caught errors
$errorHandler->setLogger($log);

统计信息

  • 总下载量: 10.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

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