如何在 Apigility 中使用 validation_messages 和 display_exceptions?

How to use validation_messages and display_exceptions in Apigility?

来自 Apigility 文档 (Error Reporting):

The API Problem specification allows you to compose any other additional fields that you feel would help further clarify the problem and why it occurred. Apigility uses this fact to provide more information in several ways:

  • Validation error messages are reported via a validation_messages key.
  • When the display_exceptions view configuration setting is enabled, stack traces are included via trace and exception_stack properties.

我不明白文档的这一部分。 目的是什么如何使用设置validation_messagesdisplay_exceptions

display_exceptions 设置来自 ZF2 的视图管理器 (see docs here)。打开它会导致 Apigiltiy 包含一个堆栈跟踪和任何错误响应。

在 Apigility 本身中,validation_messages 键填充是自动处理的。您配置一个输入过滤器来验证传入的数据有效负载,如果输入过滤器失败,错误消息 returns 会自动注入到 API 键下的 API 响应中。此功能由模块 zf-content-validation 提供。您可以通过从您的资源中返回 ApiProblemResponse 来 "do it yourself",如下所示:

return new ApiProblemResponse(
    new ApiProblem(422, 'Failed Validation', null, null, array(
        'validation_messages' => [ /* array of messages */ ]
    ))
);