0

I didn't find the way how to describe all my response codes, and I see only default ones. I have a lot of responses, and want to describe them. Also I'm interested in describing different requests which cause 400 response error(for example that request with such kind of data will return that message and so on), should it be described in API documentation?

albert
  • 8,285
  • 3
  • 19
  • 32
Grokking
  • 665
  • 8
  • 16

2 Answers2

0

How to customize error response in Apigility

Apigility works seamlessly together with ZF Api Problem for error handling.

Creating an error response from a controller or listener is as simple as:

use ZF\ApiProblem\ApiProblem;

...

return new ApiProblem(500, "My Internal Server Error");

The status titles for common errors are set here in the class.

It is advisable to stick to valid http error codes that suit the problem that occurs, but you can of course customize the title. You can return your ApiProblem directly from your controllers and listeners, Apigility will properly handle the error and return a rendered json response with Content-Type headers set to application/problem+json.

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Yep, I've done exactly what you wrote. I've created custom API response class, inherited from Apigility's ApiProblem, added custom error codes as constants, and use them in whatever I need it. It's looks like: return new ApiProblem(400, CustomResponseClass::SomeProblem); – Grokking Feb 24 '15 at 09:43
0

not sure if this is still of actuality, but you could make use of the constructor parameter additional in the Api response object to customize your answers;

return new ApiProblem(
    Response::STATUS_CODE_500,
    'Internal Server Error',
    null,
    'This is the title',
    [
        'specific' => 'entry',
        'more_problem_details' => [
            'custom_code' => 'custom_code_value',
            'custom_message' => 'custom_message_text',
        ],
    ]
);

The response would then look like this:

{
    "specific": "entry",
    "more_problem_details": {
        "custom_code": "custom_code_value",
        "custom_message": "custom_message_text"
    },
    "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
    "title": "This is the title",
    "status": 500,
    "detail": "Internal Server Error"
}

Check this for more details:

https://docs.zendframework.com/zend-problem-details/response/

dickwan
  • 337
  • 4
  • 13