Search code examples
error-handlingexceptionzend-framework2laminas-api-tools

How to use validation_messages and display_exceptions in Apigility?


From the Apigility documentation (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.

I don't understand this part of the docu. What is the purpose and how to use the settings validation_messages and display_exceptions?


Solution

  • The display_exceptions setting is from ZF2's view manager (see docs here). Turning this on will cause Apigiltiy to include a stack trace with any error response.

    In Apigility itself the validation_messages key population is handled automatically. You configure an input filter which validates the incoming data payload and if the input filter fails the error messages it returns are automatically injected into the API response under the validation_messages key. This functionality is provided by the module zf-content-validation. You can "do it yourself" by returning an ApiProblemResponse from your resource like so:

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