juliangut/slim-exception 问题修复 & 功能扩展

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

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

juliangut/slim-exception

Composer 安装命令:

composer require juliangut/slim-exception

包简介

Slim HTTP exceptions and exception handling

README 文档

README

PHP version Latest Version License

Total Downloads Monthly Downloads

slim-exception

Alternative Slim error handling with better response format negotiation, better exception logging and better development support

Installation

Composer

composer require juliangut/slim-exception

Usage

Require composer autoload file

require './vendor/autoload.php';

use Jgut\Slim\Exception\Handler\ErrorHandler;
use Jgut\Slim\Exception\Whoops\Handler\ErrorHandler as WhoopsErrorHandler;
use Negotiation\Negotiator;
use Slim\Factory\AppFactory;
use Whoops\Run as Whoops;

// Instantiate the app
$app = AppFactory::create();

// ...

$callableResolver = $app->getCallableResolver();
$responseFactory = $app->getResponseFactory();
$logger = new Logger();

$errorHandler = $inDevelopment && class_exists(WhoopsErrorHandler::class)
    ? new WhoopsErrorHandler($callableResolver, $responseFactory, new Negotiator(), $logger)
    : new ErrorHandler($callableResolver, $responseFactory, new Negotiator(), $logger);

// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($inDevelopment, true, true);
$errorMiddleware->setDefaultErrorHandler($errorHandler);

// ...

$app->run();

Renderers

Custom error renderers are configured when using slim-exception error handlers. Fear not, out of the box ErrorHandler is a direct drop-in to change default Slim ErrorHandler

You can register your error renderers or completely change them

$errorHandler = new ErrorHandler($callableResolver, $responseFactory, new Negotiator());

// Set single error renderer
$errorHandler->setErrorRenderer('application/xhtml+xml', MyCustomHtmlRenderer::class);

// Completely replace error renderers
$errorHandler->setErrorRenderers(['text/html' => MyCustomHtmlRenderer::class]);

Whoops

Developers deserve a better and more informative error handling while in development environment

Whoops is a great tool for this purpose and its usage is integrated in this package. There is a special Whoops error handler which can be used as default exception handler for development

Given Whoops renderers are meant for development displayErrorDetails argument on Slim\Interfaces\ErrorRendererInterface::__invoke won't be considered and stacktrace will always be displayed

The example of how to include Whoops error handler is in the code above

For you to use this handler you'll need to require whoops first. Additionally, Symfony's var-dumper plays nice with whoops so require it too

composer require --dev filp/whoops
composer require --dev symfony/var-dumper

Handle all errors/exceptions

In order to fully integrate error handling with the environment you can register ExceptionHandler globally. In this way any triggered and unhandled error will be captured and treated by the error handler

use Jgut\Slim\Exception\ExceptionHandler;
use Slim\Factory\AppFactory;

// Instantiate the app
$app = AppFactory::create();

// ...
// Create and register $errorHandler in error middleware

$request = Psr17ServerRequestFactoryInterface::createServerRequest();

$exceptionHandler = new ExceptionHandler($request, $errorHandler, $inDevelopment, true, true);
$exceptionHandler->registerHandling();

// ...

$app->run($request);

// This error will be captured and gracefully handled
trigger_error('This is embarrassing', \E_USER_ERROR);

Upgrade from 2.x

  • Minimum PHP version is now 8.0
  • Minimum Whoops version is now 2.15 as custom Inspector has been removed in favor of Whoop's frame filters

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.

juliangut/slim-exception 适用场景与选型建议

juliangut/slim-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.83k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 juliangut/slim-exception 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-07-30