nextdots/api-exceptions

API 的 JSON 异常

dev-master 2017-01-19 03:41 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:10:28 UTC


README

将您的 Lumen 异常转换为 JSON

{
  "code": nnn,
  "message": "a message",
  "description": "a description",
  "errors": [
    "error 1",
    "error 2",
    ...
  ]
}

安装

将您的 composer.json 设置为允许更不稳定的包

"minimum-stability" : "dev",  
"prefer-stable" : true

按常规要求该包

composer require nextdots/api-exceptions

更改 app/Exceptions/Handler.php

  • 更改这个

    use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
  • 为这个

    use ApiExceptions\Handler as ExceptionHandler;

在 Linux 上,您可以使用以下命令执行与上述相同的操作

sed -i 's/Laravel\\Lumen\\Exceptions\\Handler/ApiExceptions\\Handler/' app/Exceptions/Handler.php

示例

use ApiExceptions\JsonResponseException;

$app->get('/throw-exception', function () {
    throw new JsonResponseException(400, "an exception", "a description");
});