4

我正在使用 Slim3 开发一个相当大的 JSON API。我的控制器/动作目前充斥着以下内容:

return $response->withJson([
    'status' => 'error',
    'data' => null,
    'message' => 'Username or password was incorrect'
]);

在应用程序的某些点上,任何事情都可能出错,因此需要做出适当的响应。但常见的一件事是错误响应总是相同的。status总是error,是可选的data(在表单验证错误的情况下data将包含这些),并message设置为向 API 的用户或使用者指示出了什么问题。

我闻到代码重复的味道。如何减少代码重复?

从我的脑海中,我所能想到的就是创建一个自定义异常,类似这样的App\Exceptions\AppException事情需要选项data并且message将获得表单$e->getMessage()

<?php

namespace App\Exceptions;

class AppException extends Exception
{
    private $data;

    public function __construct($message, $data = null, $code = 0, $previous = null)
    {
        $this->data = $data;
        parent::__construct($message, $code, $previous);
    }

    public function getData()
    {
        return $this->data;
    }
}

之后创建中间件,调用$next包装在 try/catch 中:

$app->add(function($request, $response, $next) {

  try {
    return $next($request, $response);
  }
  catch(\App\Exceptions\AppException $e)
  {
    $container->Logger->addCritical('Application Error: ' . $e->getMessage());
    return $response->withJson([
      'status' => 'error',
      'data' => $e->getData(),
      'message' => $e->getMessage()
    ]);
  }
  catch(\Exception $e)
  {
    $container->Logger->addCritical('Unhandled Exception: ' . $e->getMessage());
    $container->SMSService->send(getenv('ADMIN_MOBILE'), "Shit has hit the fan! Run to your computer and check the error logs. Beep. Boop.");
    return $response->withJson([
      'status' => 'error',
      'data' => null,
      'message' => 'It is not possible to perform this action right now'
    ]);
  }
});

现在我在代码中所要做的就是throw new \App\Exceptions\AppException("Username or password incorrect", null).

我唯一的问题是感觉我使用异常是出于错误的原因,这可能会使调试变得更加困难。

关于减少重复和清理错误响应的任何建议?

4

1 回答 1

5

您可以通过创建输出 JSON 的错误处理程序来获得类似的结果。

namespace Slim\Handlers;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

final class ApiError extends \Slim\Handlers\Error
{
    public function __invoke(Request $request, Response $response, \Exception $exception)
    {
        $status = $exception->getCode() ?: 500;
        $data = [
            "status" => "error",
            "message" => $exception->getMessage(),
        ];
        $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
        return $response
                   ->withStatus($status)
                   ->withHeader("Content-type", "application/json")
                   ->write($body);
    }
}

您还必须配置 Slim 以使用您的自定义错误处理程序。

$container = $app->getContainer();

$container["errorHandler"] = function ($container) {
    return new Slim\Handlers\ApiError;
};

检查Slim API Skeleton以获取示例实现。

于 2016-12-08T16:08:40.273 回答