Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not get exception in JSON format #57

Open
koroan opened this issue Oct 11, 2022 · 2 comments
Open

Can not get exception in JSON format #57

koroan opened this issue Oct 11, 2022 · 2 comments
Labels
Question Further information is requested

Comments

@koroan
Copy link

koroan commented Oct 11, 2022

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?

@koroan koroan added the Bug Something isn't working label Oct 11, 2022
@Ocramius Ocramius added Question Further information is requested and removed Bug Something isn't working labels Oct 11, 2022
@Ocramius
Copy link
Member

@weierophinney you may have better insight here?

@weierophinney
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants