onramplab/laravel-exceptions

处理 Laravel 异常以记录日志和 API 响应

v1.3.0 2023-12-08 10:01 UTC

This package is auto-updated.

Last update: 2024-09-08 11:36:15 UTC


README

Software License CircleCI Total Downloads

如果您正在尝试创建一个新的 PHP Composer 包,无论是要提交到 packagist.org 还是仅在您的 Github 账户中存在,这个文件模板包一定会帮助您使这个过程更加容易和快速。

要求

  • PHP >= 8.1;
  • composer.

特性

  • 遵循 JSON API 规范
  • 为错误添加更多上下文到日志
    • 适配器
      • Web
      • 控制台
      • 作业

API 错误响应示例

{
  "errors": [
    {
      "title": "Resource Not Found",
      "detail": "User Not Found",
      "message": "User Not Found",
      "status": 400
    }
  ]
}

错误日志示例

Here is the example of error log context:

```json
{
  "detail": "A fake message",
  "adapter": {
    "type": "API",
    "route": "test-route",
    "method": "GET",
    "url": "https:///test-route",
    "input": []
  },
  "errors": [
    {
      "title": "Unable To Do Something",
      "detail": "A fake message",
      "exception_class": "OnrampLab\\CleanArchitecture\\Exceptions\\UseCaseException",
      "stacktrace": [
        "## /var/www/html/tests/Unit/Exceptions/HandlerTest.php(149)",
        "#0 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(1548): OnrampLab\\CleanArchitecture\\Tests\\Unit\\Exceptions\\HandlerTest->handleUseCaseException2()"
      ]
    },
    {
      "title": "Fake Domain Exception",
      "detail": "A fake message",
      "exception_class": "OnrampLab\\CleanArchitecture\\Tests\\Unit\\Exceptions\\FakeDomainException",
      "stacktrace": [
        "## /var/www/html/tests/Unit/Exceptions/HandlerTest.php(146)",
        "#0 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(1548): OnrampLab\\CleanArchitecture\\Tests\\Unit\\Exceptions\\HandlerTest->handleUseCaseException2()"
      ]
    }
  ]
}

## Tech Features

- PSR-4 autoloading compliant structure;
- PSR-2 compliant code style;
- Unit-Testing with PHPUnit 6;
- Comprehensive guide and tutorial;
- Easy to use with any framework or even a plain php file;
- Useful tools for better code included.

## Installation

```bash
composer require onramplab/laravel-exceptions

实用工具

运行测试

php vendor/bin/phpunit

composer test

代码检查工具

php vendor/bin/phpcs --standard=PSR2 src/

composer psr2check

代码自动修复工具

composer psr2autofix
composer insights:fix
rector:fix

构建文档

php vendor/bin/phpdoc -d "src" -t "docs"

composer docs

变更日志

请参考 CHANGELOG.md 以保持跟踪。

贡献

  1. 将其分叉。
  2. 创建您的功能分支(git checkout -b my-new-feature)。
  3. 进行更改。
  4. 运行测试,如有必要为您的代码添加新测试(phpunit)。
  5. 提交您的更改(git commit -am '添加一些特性')。
  6. 将更改推送到分支(git push origin my-new-feature)。
  7. 创建新的拉取请求。

也请参考 CONTRIBUTION.md

许可

请参考 LICENSE