mnavarrocarter/problem-details
Composer 安装命令:
composer require mnavarrocarter/problem-details
包简介
A framework agnostic RFC7807 implementation
README 文档
README
ApiException is an abstract exception class for php that implements the RFC7807 problem details structure.
It is ment to be extended into other exception classes that define specific error types, according to the RFC.
Installing
Simply do:
composer require mnavarrocarter/problem-details
Usage
Creating your custom Exception Types
For the different problem types in your api, you must create meaningful exception classes that will extend this abstract one, and override the constructor to set your custom type, title and, if you want, status code.
<?php namespace App\Errors; use MNC\ProblemDetails\ApiException; class AuthenticationProblemException extends ApiException { public function __construct(string $detail = '', array $extra = [], ?\Throwable $previous = null) { $type = 'errors/authentication'; $title = 'Authentication Error'; $statusCode = 401; parent::__construct($type, $title, $statusCode, $detail, $extra, $previous); } }
Using Factory Pattern to simplify error creation.
Now, with your class created, you can use the factory pattern to quickly and simply create new instances of the custom error type you just created.
<?php namespace App\Errors; use MNC\ProblemDetails\ApiException; class AuthenticationProblemException extends ApiException { public function __construct(string $detail = '', array $extra = [], ?\Throwable $previous = null) { $type = 'errors/authentication'; $title = 'Authentication Error'; $statusCode = 401; parent::__construct($type, $title, $statusCode, $detail, $extra, $previous); } public static function invalidCredentials(array $sentCredentials) { return new self( 'Invalid credentials', $sentCredentials ); } }
Then, in your client code:
<?php namespace App\Services; use App\Error\AuthenticationProblemException; class Authenticator extends ApiException { public function authenticate(array $sentCredentials) { if ($this->areValidCredentials($sentCredentials)) { return $this->findUser($sentCredentials); } throw AuthenticationProblemException::invalidCredentials($sentCredentials); } }
Then, at your controller level, you can catch these exceptions to provide a response. Or, if you use frameworks like Symfony, you can configure an Exception Listener.
<?php namespace App\Controller; use MNC\ProblemDetails\ApiException; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class SomeController extends Controller { public function someAction(Request $request) { try { $variable = $this->tryAction($request); } catch (ApiException $e) { return $this->json($e, $e->getStatusCode()); } return $variable; } }
If you noticed, the ApiException implements PHP's \JsonSerializable interface. So
it is easily serialized with a simple json_encode(). If you want to just normalize
the object, you can call the toArray() method.
Modifiying the implementation
tldr; You can't. You can implement the interface though.
This implementation is very closed. Property visibility is hidden from you, and you can only interact with the properties via the public methods.
Also, the implementation methods are declared final, so you can't override them. This will protect you from breaking anything, as this class is developed to always provide a meaningful response by checking stuff in getters and setters.
The interface is there in case you want to create your own implementation.
mnavarrocarter/problem-details 适用场景与选型建议
mnavarrocarter/problem-details 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mnavarrocarter/problem-details 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mnavarrocarter/problem-details 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-04