微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ApiPlatform 序列化异常

如何解决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 举报,一经查实,本站将立刻删除。