ApiPlatform 序列化异常

问题描述

我使用 ApiPlatform 并且我的域 (DDD) 可以抛出类似的异常

final class QuotaExceededException extends \Exception
{
    public function __construct(
        private int $cost,private int $quotaLeft,string $message
    ) {
        parent::__construct($message);
    }

    public function getCost(): int
    {
        return $this->cost;
    }

    public function getQuotaLeft(): int
    {
        return $this->quotaLeft;
    }
}

我找到了如何为该异常自定义 http 代码,但我还想返回一个有效负载,如

{
    "type": "https:\/\/tools.ietf.org\/html\/rfc2616#section-10","title": "An error occurred","detail": "The message thrown","cost": 10,"quotaLeft": 2
}

我尝试实现特定的 normalizer (QuotaExceededExceptionnormalizer) 但似乎每个抛出的异常都被转换为 FlattenException 只保留异常消息和 costquotaLeft 信息丢失了。

你有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)