laminas-api-tools / api-tools-skeleton

Skeleton Application for Laminas API Tools
https://api-tools.getlaminas.org/documentation
BSD 3-Clause "New" or "Revised" License
50 stars 64 forks source link

Can not get exception in JSON format #57

Open koroan opened 2 years ago

koroan commented 2 years ago

Bug Report

Q A
Version(s) 1.8.0

Summary

I want to throw exception inside Rpc controller, like this:

$ex = new \Laminas\ApiTools\ApiProblem\Exception\DomainException('The request you made was malformed', 400);
$ex->setType('/documentation/problems/malformed-request');
$ex->setTitle('Malformed Request');
$ex->setAdditionalDetails([
    'missing-sort-direction' => 'The sort direction query string was missing and is required'
]);
throw $ex;

And I got an exception in html format (/module/Application/view/error/index.phtml works), but I was expecting response in JSON, like this:

HTTP/1.1 500 Internal Error
Content-Type: application/problem+json

{
    "type": "/documentation/problems/malformed-request",
    "detail": "The request you made was malformed",
    "status": 400,
    "title": "Malformed Request",
    "missing-sort-direction": "The sort direction query string was missing and is required"
}

Of course I have header Content-Type: application/json What I do incorrect? How can I get exception in Json format?

Ocramius commented 2 years ago

@weierophinney you may have better insight here?

weierophinney commented 2 years ago

First, is the controller listed in the api-tools-api-problem.render_error_controllers configuration key? (see https://api-tools.getlaminas.org/documentation/modules/api-tools-api-problem#key-render_error_controllers)

Second, have you tried returning an ApiProblemResponse from your controller instead of throwing the exception? The documentation I linked demonstrates that, and it's an alternative to throwing the exception.